setting a value in an associative array of associative arrays using variables Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Gibbons
Posts: 87
Joined: 20 Sep 2016, 20:39

setting a value in an associative array of associative arrays using variables

23 Jun 2018, 15:14

What is the correct syntax for "person[k1][k2] := "purple" below?
(I know this example doesn't make a lot of sense since everything would be set to "purple")

Code: Select all

Jack := {profession: "teacher"
         , height: "tall"
         , country: "USA"
         , city: "New York"}

Paul := {profession: "cook"
         , height: "short"
         , country: "UK"
         , city: "London"}

Bill := {profession: "designer"
         , height: "short"
         , country: "Canada"
         , city: "Toronto"}

Max := {profession: "driver"
        , height: "tall"
        , country: "USA"
        , city: "Dallas"}

Bill := {profession: "policeman"
         , height: "tall"
         , country: "Australia"
         , city: "Canberra"}

Person := {Jack: Jack
           , Paul: Paul
           , Bill: Bill
           , Max: Max
           , Bill: Bill}

{
        for k1,v1 in person
        {
                for k2,v2 in person[k1]
                {
                        person[k1][k2] := "purple"
                        msgbox, %k1%   %k2%    %v2%
                }
        }
}
Thanks!
User avatar
TLM
Posts: 1580
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 15:45

Not sure I understand the question 100% but you can set a value like this:

Code: Select all

Person := { Jack: ""
           , Paul: ""
           , Bill: ""
           , Max: ""
           , Bill: "" }

Person.Max := "Purple" ; sets the key Max to the value "Purple"

for k1,v1 in person
{
	msgbox % k1 ": " v1
}
Gibbons
Posts: 87
Joined: 20 Sep 2016, 20:39

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 15:52

TLM wrote:Not sure I understand the question 100%
I'm trying to set the value within the double for loop, thus I'd only have the two variables to work with. I'm just looking for the syntax of the line I quoted in my question.
User avatar
TLM
Posts: 1580
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 16:03

Gibbons wrote:I know this example doesn't make a lot of sense
That's the thing, the script will never iterate through the second loop because there is no "k2" key in Person.
What specific Key Value(s) are you trying to set to `Purple` ?
gregster
Posts: 3855
Joined: 30 Sep 2013, 06:48

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 16:05

First of all, you define the associative Bill twice, effectively overwriting it: Bill the policeman overwrites Bill the designer.

But there are different options available:

Code: Select all

Jack := {profession: "teacher"
         , height: "tall"
         , country: "USA"
         , city: "New York"}

Paul := {profession: "cook"
         , height: "short"
         , country: "UK"
         , city: "London"}

Bill := {profession: "designer"
         , height: "short"
         , country: "Canada"
         , city: "Toronto"}

Max := {profession: "driver"
        , height: "tall"
        , country: "USA"
        , city: "Dallas"}

Bill2 := {profession: "policeman"
         , height: "tall"
         , country: "Australia"
         , city: "Canberra"}

Person := {Jack: Jack
           , Paul: Paul
           , Bill: Bill
           , Max: Max
           , Bill2: Bill2}

Person.Jack.color := "green"
msgbox % "Jack - color: " Person.Jack.color

Person["Max"].color := "red"
msgbox % "Max - color: " Person["Max"].color

Person["Max"]["color"] := "blue"
msgbox % "Max - color: " Person["Max"]["color"]

for name, obj in person
{
	obj.color := "purple"
	for key, val in obj
        msgbox, % name " - " key " : " val
}
gregster
Posts: 3855
Joined: 30 Sep 2013, 06:48

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 16:22

But if you really want to set the value of every key to "purple", you can do this:

Code: Select all

for name, obj in person
{
	for key, val in obj		; obj is the specific assoc. array (Bill, Bill2, ...) - key is for example "profession", value then "teacher" etc.
	{
       	obj[key] := "purple"		; assign "purple" to the specific key of the assoc. array obj 
		msgbox, % name " - " key " : " obj[key] "      - old value: " val  
	}
}
val still holds the original values during the loop. Hence, use obj[key] to see the newly assigned values ( aka "purple").
Gibbons
Posts: 87
Joined: 20 Sep 2016, 20:39

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 17:56

I am having difficulty communicating today and I'm making things too difficult.

My main question: How would I write:

Person["Max"]["color"] := "blue"

if I was using k1 is Max and k2 is color

where both Max and color are non-quoted keys in an associative array of associative arrays so I end up with Person[Max][color].

I appreciate all the help, hopefully I'm communicating my question well now.
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: setting a value in an associative array of associative arrays using variables  Topic is solved

23 Jun 2018, 18:21

Code: Select all

Jack := {profession: "teacher"
         , height: "tall"
         , country: "USA"
         , city: "New York"}

Paul := {profession: "cook"
         , height: "short"
         , country: "UK"
         , city: "London"}

Bill := {profession: "designer"
         , height: "short"
         , country: "Canada"
         , city: "Toronto"}

Max := {profession: "driver"
        , height: "tall"
        , country: "USA"
        , city: "Dallas"}

Bill := {profession: "policeman"
         , height: "tall"
         , country: "Australia"
         , city: "Canberra"}

Person := {Jack: Jack
           , Paul: Paul
           , Bill: Bill
           , Max: Max
           , Bill: Bill}

for k1,v1 in Person
{
	for k2,v2 in v1
	{
		Person[k1][k2] := "purple"
		MsgBox % k1 " " k2 " = " Person[k1][k2]
	}
}
Since there is no condition - currently all subkeys (city, etc.) are overwritten with "purple". You can assign color first, then read all of them with following:

Code: Select all

for k1,v1 in Person
{
	Person[k1]["color"] := "purple"
	for k2,v2 in v1
	{
		MsgBox % k1 " " k2 " = " Person[k1][k2]
	}
}
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
gregster
Posts: 3855
Joined: 30 Sep 2013, 06:48

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 18:36

If the quotes are the problem, then first you have to realize that non-quoted keys don't mean anything with string keys, if you are defining an assoc. array - here, quotation marks are just options, but it will create the same object.
https://autohotkey.com/docs/Objects.htm#Usage_Associative_Arrays wrote: Using the {key:value} notation, quote marks are optional for keys which consist only of word characters.
(https://autohotkey.com/docs/Objects.htm ... ive_Arrays)

Thus,

Code: Select all

Jack := {profession: "teacher"}
is exactly the same like

Code: Select all

Jack := {"profession": "teacher"}
.

But you can use only the quoted string key when you want to return the values (if you use the [ ] notation). To get "teacher" you have to use Jack["profession"] - but Jack[profession]: this will look for a variable named profession, outside the array, in the general namespace.
k1 is Max and k2 is color

Code: Select all

Max := {profession: "teacher"
         , height: "tall"
         , country: "USA"
         , color: "green"}
Person :={Max : Max }
k1 := "Max"
k2 := "color"
msgbox % Person[k1][k2]
Or, is it that you misunderstand how the scope of the for-loop works? In this case, please review all examples above.
User avatar
TLM
Posts: 1580
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: setting a value in an associative array of associative arrays using variables

23 Jun 2018, 19:31

My apologies. I originally ran the code from the Person array onward.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], flyingDman, Google [Bot], honarjoo, Odlanir, WOlfen and 107 guests