Trouble understanding associative arrays

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Trouble understanding associative arrays

Post by PuzzledGreatly » 02 Jul 2022, 19:35

Why is my message box in the following code blank? Thanks

Code: Select all

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

gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Trouble understanding associative arrays

Post by gregster » 02 Jul 2022, 19:52

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.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Trouble understanding associative arrays

Post by PuzzledGreatly » 02 Jul 2022, 19:59

Thanks gregster, you beat me to it. I just worked it out and was returning to post the same.

Post Reply

Return to “Ask for Help (v1)”