AutoHotKey: How to access array with counter variable

怎甘沉沦 提交于 2019-12-11 03:15:58

问题


; 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


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/25772799/autohotkey-how-to-access-array-with-counter-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!