Page 1 of 1

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

Posted: 23 Jun 2018, 15:14
by Gibbons
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!

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

Posted: 23 Jun 2018, 15:45
by TLM
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
}

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

Posted: 23 Jun 2018, 15:52
by Gibbons
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.

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

Posted: 23 Jun 2018, 16:03
by TLM
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` ?

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

Posted: 23 Jun 2018, 16:05
by gregster
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
}

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

Posted: 23 Jun 2018, 16:22
by gregster
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").

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

Posted: 23 Jun 2018, 17:56
by Gibbons
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.

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

Posted: 23 Jun 2018, 18:21
by SirRFI

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]
	}
}

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

Posted: 23 Jun 2018, 18:36
by gregster
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.

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

Posted: 23 Jun 2018, 19:31
by TLM
My apologies. I originally ran the code from the Person array onward.

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

Posted: 23 Jun 2018, 22:00
by Gibbons
Thanks everyone!