首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AutoHotKey阵列混淆

AutoHotKey阵列混淆
EN

Stack Overflow用户
提问于 2014-10-08 14:31:23
回答 1查看 4.3K关注 0票数 1

我想问一下下面的代码是两个不同的数组吗?还是同一个数组?在AutoHotKey中,我很困惑如何创建两个不同的数组。我使用了一个非常旧的版本的AutoHotKey,v1.0.47.06.

代码语言:javascript
运行
复制
; 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

而且,如果我有一个变量,我使用=将它赋值给数组?还是使用:=?

代码语言:javascript
运行
复制
discharge_date := "01/01/2014"
Array%date_array_count% = discharge_date
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-08 15:06:37

这些不是真正的“数组”,它们被称为pseduo数组,是创建数组的一种旧方法。你可以读到更多关于他们的这里

在你的例子中,它们是相同的数组,是的。您使用Array作为数组变量,使用date_array_countcase_array_count作为索引。这两个值都是零,所以将这两个值放在相同的索引中,这意味着您将覆盖第一个值。

至于赋值,请尝试始终使用:=。当您想要分配一个字符串时,引用如下所示的字符串:myVariable := "Hello world!"

"Real“数组是在更晚的版本中添加到AHK中的,我强烈建议您升级到这个版本。您可以从这里获得最新版本- http://ahkscript.org/

一旦您有了最新版本,您就可以阅读更多关于docs - http://ahkscript.org/docs/misc/Arrays.htm中数组的信息。

下面是如何使用数组的一个基本示例:

代码语言:javascript
运行
复制
; 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%
}

您还可以为数组赋值如下:

代码语言:javascript
运行
复制
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

编辑:

如果您无法升级,这就是实现和使用多个pseduo数组的方法。

代码语言:javascript
运行
复制
; 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%
}

如果您想要数组共享相同的索引:

代码语言:javascript
运行
复制
; 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%
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26259516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档