Using Variables to retreive values from an array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Mandragoran
Posts: 6
Joined: 03 Feb 2016, 15:52

Using Variables to retreive values from an array

08 Feb 2016, 13:27

I am trying to retrieve a value from an associative array using a variable. see below

Code: Select all

Array := {Red_485: "C:\...\A PICTURES\RED 485.jpg", Reflex_Blue: "C:\...\A PICTURES\REFLEX BLUE", Blue_801: "C:\...\A PICTURES\BLUE 801", Green_802: "C:\...\A PICTURES\GREEN 802", Yellow_803: "C:\...\A PICTURES\YELLOW 803", Orange_804: "C:\...\A PICTURES\ORANGE 804", Orange_805: "C:\...\A PICTURES\ORANGE 805", Pink_806: "C:\...\A PICTURES\PINK 806", Purple_807: "C:\...\A PICTURES\PURPLE 807", Silver_877: "C:\...\A PICTURES\SILVER 877"}


^6:: 
VAR1:= Red_485
Send % Array["VAR1"] ; IS THERE A SYNTAX ERROR HERE?
return
If someone could help me out or point me to a tutorial that explains how to do this that would rock.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Using Variables to retreive values from an array

08 Feb 2016, 13:31

You are passing the string "VAR1" instead of the contents of the variable.

Try:

Code: Select all

Send % Array[VAR1]
User avatar
boiler
Posts: 17384
Joined: 21 Dec 2014, 02:44

Re: Using Variables to retreive values from an array

08 Feb 2016, 13:37

You also need to assign VAR1 to have the value of "Red_485" as text. The way you currently have it, you assigned it the value of the variable named Red_485, which has not been defined so it has a null value. Change your assignment to this:

VAR1:= "Red_485"

Demonstration:

Code: Select all

Array := {Red_485: "C:\...\A PICTURES\RED 485.jpg", Reflex_Blue: "C:\...\A PICTURES\REFLEX BLUE", Blue_801: "C:\...\A PICTURES\BLUE 801", Green_802: "C:\...\A PICTURES\GREEN 802", Yellow_803: "C:\...\A PICTURES\YELLOW 803", Orange_804: "C:\...\A PICTURES\ORANGE 804", Orange_805: "C:\...\A PICTURES\ORANGE 805", Pink_806: "C:\...\A PICTURES\PINK 806", Purple_807: "C:\...\A PICTURES\PURPLE 807", Silver_877: "C:\...\A PICTURES\SILVER 877"}
 
VAR1:= "Red_485"
MsgBox % Array[VAR1]
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: adam86shadow, balawi28, Google [Bot] and 240 guests