Associative Array with variable as key - beginner question Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
doctorafterman
Posts: 24
Joined: 26 Aug 2019, 08:45

Associative Array with variable as key - beginner question

17 Oct 2019, 09:56

I'm new to associative arrays/objects, I'm trying to use a variable to lookup the key in the array. I'm expecting the MsgBox to return either Y or N (or a number in the future) based on the user input of a two letter state abbreviation.

This is returning blank right now:

Code: Select all

StatesArray := {AL: N, AR: N, AZ: N, CO: N, FL: N, GA: Y, IA: N, IL: Y, IN: Y, KS: Y, KY: N, LA: N, MD: Y, MI: Y, MO: Y, MS: N, MT: N, NC: Y, NE: N, NV: Y, OH: Y, OK: N, PA: N, SC: Y, TN: N, TX: Y, UT: Y, VA: Y, WV: N, WY: N}

^!l::
InputBox State
StatePosition := StatesArray[%State%]
MsgBox %StatePosition%
return 
I appreciate your help, I'm new to objects.
Hellbent
Posts: 731
Joined: 23 Sep 2017, 13:34

Re: Associative Array with variable as key - beginner question  Topic is solved

17 Oct 2019, 10:08

It looks like you have everything right except that you need to change the values to strings

A quick way you can do the without going over everything in your list and adding quotes is to just declare a variable with the same name

Code: Select all

N:="N"
Y:="Y"
You could have other issues (I only glanced at your code), but I think that should do it.


***EDIT***

Remove the "%" from StatePosition := StatesArray[%State%]
doctorafterman
Posts: 24
Joined: 26 Aug 2019, 08:45

Re: Associative Array with variable as key - beginner question

17 Oct 2019, 10:25

Great thank you, the quotes make it more readable in Notepad++ anyway.

This worked perfectly:

Code: Select all

StatesArray := {AL: "N", AR: "N", AZ: "N", CO: "N", FL: "N", GA: "Y", IA: "N", IL: "Y", IN: "Y", KS: "Y", KY: "N", LA: "N", MD: "Y", MI: "Y", MO: "Y", MS: "N", MT: "N", NC: "Y", NE: "N", NV: "Y", OH: "Y", OK: "N", PA: "N", SC: "Y", TN: "N", TX: "Y", UT: "Y", VA: "Y", WV: "N", WY: "N"}

^!l::
InputBox State
StatePosition := StatesArray[State]
MsgBox %StatePosition%
return 
Hellbent wrote:
17 Oct 2019, 10:08
It looks like you have everything right except that you need to change the values to strings

A quick way you can do the without going over everything in your list and adding quotes is to just declare a variable with the same name

Code: Select all

N:="N"
Y:="Y"
You could have other issues (I only glanced at your code), but I think that should do it.


***EDIT***

Remove the "%" from StatePosition := StatesArray[%State%]

Return to “Ask For Help”

Who is online

Users browsing this forum: AmDeG 11, Bing [Bot], Google [Bot], grimboto, loek6000, MannyKSoSo, SuperFoobar and 195 guests