2
; Declare the window_title_array
window_title_array%1% = 3270 Display A - A
window_title_array%2% = 3270 Display A - B
window_title_array%3% = 3270 Display A - C
window_title_array%4% = 3270 Display A - D
window_title_array%5% = 3270 Display A - E
window_title_array%6% = 3270 Display A - F

counter := 1

my_string := window_title_array%counter%

MsgBox, %my_string%

How to get the string from the array with a counter variable? I tried to do counter = 1 and counter := 1. Both of them failed to access the array. I am not sure where is the mistake. Thank you!

PS: The version I have is quite outdated - Version 1.0.47.06

2 Answers 2

2

I believe it's in how you're creating your array. By putting percent signs around your array indexes, you're actually saying that you want to use the first file-level input parameter (in the case of using %1%). This is most likely blank, so what it ends up looking for is a variable named "window_title_array"

Take out the percents. You should use this:

window_title_array1 = 3270 Display A - A
window_title_array2 = 3270 Display A - B
window_title_array3 = 3270 Display A - C
window_title_array4 = 3270 Display A - D
window_title_array5 = 3270 Display A - E
window_title_array6 = 3270 Display A - F

And not this:

window_title_array%1% = 3270 Display A - A
window_title_array%2% = 3270 Display A - B
window_title_array%3% = 3270 Display A - C
window_title_array%4% = 3270 Display A - D
window_title_array%5% = 3270 Display A - E
window_title_array%6% = 3270 Display A - F

And then if you want to reference something with a counter variable, ... (looking at your code)... you would do it exactly like you are.

And note that this is not a native array in AHK. But if you have an older version you may not be able to use native arrays. This is how arrays were done for a long time in AHK.

Furthermore, another way I deal with this is by creating a "built-in" counter/length variable and use that to dynamically number my arrays. Then that can easily be referenced in array loops etc. And notice no hand-coding of array indexes, which means you can add more or insert them without having to renumber anything. I'm often doing arrays of structures, and below is a simple example...

myArr0 = 0  ; At the end, this will hold the count of the array

myArr0++
myArr%myArr0%_firstName = John
myArr%myArr0%_lastName = Smith

myArr0++
myArr%myArr0%_firstName = Bill
myArr%myArr0%_lastName = Jones

myNames = 
; assemble a list of names, a simple example
loop, %myArr0%
{
myNames := myNames . myArr%a_index%_firstName . ", "
}

And I use the <array name>0 syntax for the counter because that's the same syntax as outputted by the stringsplit command.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is not in counter variable, both of your versions will work fine but I suggest you to always use only := in AutoHotkey. You can use expression with := and if you need to assign text to variable just enclose text with "" like this a := "Some text here". But to assign to the variable the result of expression dont use "", like this a:= 1+1. Try to not use = for assignment in AutoHotkey.
Look in comments in my code and notes below my code for explanation. Here is working code:

window_title_array := [] ; We create array here

; we are adding items to array.
window_title_array[1] := "3270 Display A - A"
window_title_array[2] := "3270 Display A - B"
window_title_array[3] := "3270 Display A - C"
window_title_array[4] := "3270 Display A - D"
window_title_array[5] := "3270 Display A - E"
window_title_array[6] := "3270 Display A - F"

counter := 1

my_string := window_title_array[counter] ; here we need [] to indicate that we are using array cell and variable incide it does not needs to be enclosed in %%

MsgBox, %my_string%

Here you can get more infor about arrays and AutoHotkey http://ahkscript.org/docs/Objects.htm#Usage
Keep in mind that all arrays in AutoHotkey are objects.
also you can declare array and add values in one string. More about it in link that a gave you above.

Also, always use AutoHotkey and its documenatation from http://ahkscript.org/ (current uptodate version, new official website)! AutoHotkey and its documentation from autohotkey.com is outdated and you may have some problems using them!

6 Comments

Is it because of the version of the AHK I have? The code you provided does not print the title of the window...
@George I have checked the codeI gave you and works fine with the current uptodate version of AutoHotkey form ahkscript.org .
@George It prints 3270 Display A - A in message box.
Unfortunately I have a really old version... Version 1.0.47.06.
@George Download new version from here ahkscript.org/download/ahk-install.exe
|

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.