Page 1 of 1

Trouble understanding associative arrays

Posted: 02 Jul 2022, 19:35
by PuzzledGreatly
Why is my message box in the following code blank? Thanks

Code: Select all

Global F := {"red":1}
msgbox, 4096, Why?,% F[1]

Re: Trouble understanding associative arrays

Posted: 02 Jul 2022, 19:52
by gregster
Because red is the key here of the key:value-pair, 1 is the value:

Code: Select all

Global F := {"red":1}
msgbox, 4096, Why?,% F["red"]		; returns 1
Via the (unique) keys, you can access the values (or you could loop through all pairs).
You could reverse current key and value, if you want "red" as the returned value.

F := {1:"red"} would be pretty much like the linear array F := ["red"] - for the latter, the index/key is implicit.

Re: Trouble understanding associative arrays

Posted: 02 Jul 2022, 19:59
by PuzzledGreatly
Thanks gregster, you beat me to it. I just worked it out and was returning to post the same.