Passing a variable as a key to obtain an associative array value

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Tommy_B
Posts: 2
Joined: 25 Sep 2019, 06:01

Passing a variable as a key to obtain an associative array value

25 Sep 2019, 06:28

I'm using and associative array to store the contents of a file line by line. As there are two pieces of information that I care about in each line, I'm breaking these out using the "|" as a delimiter. Then I'm using the array to build out a combobox within a GUI.

Code: Select all

Engagement_IN_ARRAY := []
	Loop, read, % A_ScriptDir "\Engagement_Codes.txt"  ; reading file that is within the script directory for engagement codes and description
{
	Loop, parse, A_LoopReadLine, `n, ";" ; read file line by line
		{
			Readin_ARRAY := StrSplit(A_loopReadLine, "|") ; looking for | as the delimiter

			x := Readin_ARRAY[1] ; taking the string before (engagement code) the delimiter and writing it to a variable
			y := Readin_ARRAY[2] ; taking the string after (description) the delimiter and writing it to a variable
		
			Description_IN_ARRAY[y] := x ; setting description as the key and engagement code as the value

		}	
}

After the user has selected the appropriate drop-down item from the combobox from the GUI, I want to pass that value to a loop below that will feed that variable as the key into the array to obtain the matching value. The problem that I keep running into is the call to the associative array either breaks at run time or returns a null value (current code displays a null). I've validated that the variable "Eng_desc_loop_var" is successfully passing the right information from the combobox, but the "Eng_code_loop_var" isn't appropriately pulling from the associative array. I believe this might be a syntax issue, any help would be greatly appreciated.

Code: Select all

ArrayCountC := 0
while ArrayCountC < 8
	{
		ArrayCountC += 1

		GUI_Eng_var := "Eng" . ArrayCountC ; building variable to be able to loop through GUI inputs
		
		GuiControlGet, Eng_desc_loop_var,, %GUI_Eng_var% ; defining GUI inputs as variables
		
		Eng_code_loop_var := Description_IN_ARRAY[Eng_desc_loop_var]
	}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, Google [Bot], peter_ahk and 322 guests