Referencing Associative Array of Associative Arrays Topic is solved

Ask gaming related questions (AHK v1.1 and older)
socialb
Posts: 4
Joined: 25 Sep 2022, 10:38

Referencing Associative Array of Associative Arrays

Post by socialb » 25 Sep 2022, 10:43

Code: Select all

xTable["Classic"]   :=  {"Normal": 80,  "Hard": 200,    "Nightmare": 315}
xTable["Challenge"] :=  {"Normal": 110, "Hard": 280}

MsgBox % xTable["Classic"]["Normal"]
I am trying to get the MsgBox to return 200, but can't figure out the syntax. This was as close as I could find in the Help file: https://lexikos.github.io/v2/docs/Objects.htm#Usage_Arrays_of_Arrays

Many thanks for your attention!

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Referencing Associative Array of Associative Arrays

Post by BoBo » 25 Sep 2022, 11:28

Code: Select all

;AHK v1.3x
xTable := {"Classic":"","Challenge":""}                                 ;schlüssel "Classic" und "Challenge" erstellen       
xTable["Classic"]   :=  {"Normal": 80,  "Hard": 200, "Nightmare": 315}  ;schlüsselwert für "Classic" mit assoc. array befüllen
xTable["Challenge"] :=  {"Normal": 110, "Hard": 280}                    ;schlüsselwert für "Challenge" mit assoc. array befüllen

MsgBox % xTable.Classic.Hard                                            ;abfrage array 'xtable', schlüssel 'Classic', unterschlüssel 'Hard'	;200
MsgBox % xTable.Challenge.Normal                                        ;abfrage array 'xtable', schlüssel 'Challenge', unterschlüssel 'Hard'	;110
HTH

BTw, your request is currently hosted in 'Ask for Help' (AHK v1.x) while your posted link is pointing to AHK v2.x :think:

socialb
Posts: 4
Joined: 25 Sep 2022, 10:38

Re: Referencing Associative Array of Associative Arrays

Post by socialb » 25 Sep 2022, 17:06

Thank you very much Bobo.

Happily I am using version 1.1.34.04, despite the link to a newer help file.

Your syntax works well. I was hoping for a syntax like xTable["Classic"]["Normal"] because I was hoping to use variables:

Code: Select all

xTable := {"Classic":"","Challenge":""}
xTable["Classic"]   :=  {"Normal": 80,  "Hard": 200,    "Nightmare": 315}
xTable["Challenge"] :=  {"Normal": 110, "Hard": 280}

Type:="Classic", Difficulty:="Normal"
MsgBox % xTable.Type.Difficulty
MsgBox % xTable[Type][Difficulty]
Do you know if there is an alternate syntax that would work using variables?

socialb
Posts: 4
Joined: 25 Sep 2022, 10:38

Re: Referencing Associative Array of Associative Arrays  Topic is solved

Post by socialb » 25 Sep 2022, 17:17

Figured it out - thanks to the help on https://www.autohotkey.com/docs/Objects.htm#Usage_Objects Known Limitation

Code: Select all

xTable := {"Classic":"","Challenge":""}
xTable["Classic"]   :=  {"Normal": 80,  "Hard": 200,    "Nightmare": 315}
xTable["Challenge"] :=  {"Normal": 110, "Hard": 280}

Type:="Classic", Difficulty:="Normal"

MsgBox % (xTable[Type])[Difficulty]

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Referencing Associative Array of Associative Arrays

Post by BoBo » 26 Sep 2022, 01:52

Happily I am using version 1.1.34.04, despite the link to a newer help file.
JFTR. It is NOT a newer help file. It's the help file of an AHK release that has a different syntax!

Post Reply

Return to “Gaming Help (v1)”