1

I want to ask is the following code two different array? Or is it the same array? In AutoHotKey I am confused how to create two different arrays. I use a really old version of the AutoHotKey, ver 1.0.47.06...

; Keep track of the number of discharge cases
date_array_count := 0

; Keep track of the discharge cases date
case_array_count := 0

Array%date_array_count% := "01/01/2014"
Array@case_array_count% := 1001001

And furthermore, if I have a variable, I use = to assign it to the array? Or use :=?

discharge_date := "01/01/2014"
Array%date_array_count% = discharge_date

1 Answer 1

5

Those aren't actual "arrays", they're called pseduo-arrays and are an old way to create arrays. You can read more about them here.

In your case, those would be the same array, yes. You're using Array as your array-variable, and date_array_count and case_array_count as the index. Both of which are zero, so you're putting both values at the same index, meaning you'll overwrite your first value.

As for assigning values, try to always use :=. When you want to assign a string, quote the string like this: myVariable := "Hello world!".


"Real" arrays were added to AHK in a later version, which I strongly suggest you upgrade to. You can get the latest version from here - http://ahkscript.org/

Once you have the latest version, you can read more about arrays in the docs - http://ahkscript.org/docs/misc/Arrays.htm

Here's a basic example of how to work with arrays:

; Create an array
myArray := []

; Add values to the array
myArray.insert("cat")
myArray.insert("dog")
myArray.insert("dragon")

; Loop through the array
for each, value in myArray {
    ; each holds the index
    ; value holds the value at that index
    msgBox, Ittr: %each%, Value: %value%
}

; Get a specific item from the array
msgBox % myArray[2]

; Remove a value from the array at a set index
myArray.remove(1)

; Loop through the array
for each, value in myArray {
    ; each holds the index
    ; value holds the value at that index
    msgBox, Ittr: %each%, Value: %value%
}

You can also assign values to arrays like this:

index := 1
myVariable := "my other value"

myArray[index] := "my value" ; Puts "my value" at index "1" in the array
myArray[index] := myVariable ; Puts "my other value" at index "1" in the array

Edit:

If you're unable to upgrade, this is how you achieve and work with multiple pseduo-arrays:

; Create our index variables
dateIndex := 0
caseIndex := 0

; Create our two arrays
dateArray := ""
caseArray := ""

; Increment the index before adding a value
dateIndex++
; Add values to the array using the index variable
dateArray%dateIndex% := "01/01/2014"

caseIndex++
caseArray%caseIndex% := 1001001

; Loop through the array, use the index as the loop-count
Loop % dateIndex
{
    ; A_Index contains the current loop-index
    msgBox % dateArray%A_Index%
}

Loop % caseIndex
{
    msgBox % caseArray%A_Index%
}

And if you want to arrays to share the same index:

; Create index variable
arrIndex := 0

; Create our two arrays
dateArray := ""
caseArray := ""

; Increment the index before adding a value
arrIndex++

; Add values to the array using the index variable
dateArray%arrIndex% := "01/01/2014"
caseArray%arrIndex% := 1001001

arrIndex++
dateArray%arrIndex% := "02/02/2014"
caseArray%arrIndex% := 999999

; Loop through the array, use the index as the loop-count
Loop % arrIndex
{
    ; A_Index contains the current loop-index
    msgBox % dateArray%A_Index% "`n" caseArray%A_Index%
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the great answer, but it is company software, I am unable to upgrade. In order version, is there a way to create multiple arrays?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.