AutoHotKey:如何使用计数器变量访问数组 [英] AutoHotKey: How to access array with counter variable

查看:215
本文介绍了AutoHotKey:如何使用计数器变量访问数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

; 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%

如何从带有计数器变量的数组中获取字符串?我试图做counter = 1counter := 1.他们两个都无法访问阵列.我不确定错误在哪里.谢谢!

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:我的版本已经过时-1.0.47.06版

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

推荐答案

我相信这是您创建数组的方式.通过在数组索引周围加上百分号,实际上是在说您要使用第一个文件级输入参数(在使用%1%的情况下).这很可能是空白,因此最终寻找的是一个名为"window_title_array"的变量

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

不是这个:

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.

请注意,这不是AHK中的本机数组.但是,如果您使用的是旧版本,则可能无法使用本机阵列.这就是AHK在很长一段时间内完成阵列的方式.

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 . ", "
}

我将< array name> 0语法用于计数器,因为这与stringsplit命令输出的语法相同.

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

这篇关于AutoHotKey:如何使用计数器变量访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆