Arrays


In AutoHotkey, arrays are mostly conceptual: Each array is really just a collection of sequentially numbered variables, with each variable being perceived as an element of the array. AutoHotkey does not link these variables together in any way.

In addition to StringSplit and "WinGet List", any command that accepts an OutputVar or that assigns a value to a variable can be used to create an array. The simplest example is the assignment operator (=), as shown below:

Array%j% = %A_LoopField%

Multidimensional arrays are possible by using a separator character of your choice between the indices. For example:

Array%j%_%k% := A_LoopReadLine


The following example demonstrates how to create and access an array, in this case a series of names retrieved from a text file:

; Write to the array:
ArrayCount = 0
Loop, Read, C:\Guest List.txt   ; This loop retrieves each line from the file, one at a time.
{
	ArrayCount += 1  ; Keep track of how many items are in the array.
	Array%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
}

; Read from the array:
Loop %ArrayCount%
{
	; The following line uses the := operator to retrieve an array element:
	element := Array%A_Index%  ; A_Index is a built-in variable.
	; Alternatively, you could use the "% " prefix to make MsgBox or some other command expression-capable:
	MsgBox % "Element number " . A_Index . " is " . Array%A_Index%
}