如何在autohotkey中使用数组?

时间:2013-01-29 04:41:00

标签: arrays autohotkey

我是AutoHotKey的新手并且不熟悉它的语法。我在网上搜索过但找不到有用的例子。

请看下图。

+---+---+---+---+-------------------
| a | b | c | d |                         (1)
+---+---+---+---+-------------------
              ^
              |


+---+---+---+---+-------------------
| a | b | c | d |                         (2)
+---+---+---+---+-------------------
          ^
          |


+---+---+---+---+-------------------
| a | b | c | d |                         (3)
+---+---+---+---+-------------------
  ^
  |

+---+---+---+---+-------------------
| a | b | c | d |                         (4)
+---+---+---+---+-------------------
              ^
              |

我想录制一些字符串并在其中导航。例如,程序启动时创建了一个数组my_array。如果用户按下ctrl + c,则所选文本(假设它的字符串为a)被复制到剪贴板中,并且它也被附加到my_array。如果用户按下另一个ctrl + c,则b被复制到剪贴板中,并且它也被附加到my_array。参见(1),现在a,b,c,d附加到my_array,d在剪贴板中。现在,如果用户按下alt + left_arrow,则c被复制到剪贴板,参见(2)。现在,如果用户按下alt + left_arrow 2次,则a在剪贴板中,请参阅(3)。用户可以按alt + right_arrow 3次以在其剪贴板中返回d,请参阅(4)。此时,用户仍然可以按ctrl + c将附加数据按到my_array,然后按alt + left_arrow或alt + right_arrow在数组中移动以获取数据。

实际上对我来说这很容易用其他熟悉的语言实现,但我很难在AutoHotKey中实现它。有人可以帮忙吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

以下是将复制的Excel数据放入数组的示例。

MyClipBoard=%ClipBoard% ; Remove any non-text (format) data

Loop, parse, MyClipBoard, `n,`r ; Place Excel content from clipboard in 2 dimentional Array
{ ; Start-Loop Read through each Line (record) in the ClipBoard data from Excel
  FullRecord:=A_LoopField ; Put a whole row in the variable FullRecord
  RecordNumber:=A_Index ; Store the current record (row) number.
  Loop, parse, FullRecord, %A_Tab% ; Parse the row content (FullRecord) in separate fields
  { ; Start-Loop Read through each Field in the ClipBoard data from Excel
    FieldNumber:=A_Index ; Store the current Field number.
    Array%RecordNumber%_%FieldNumber% := A_LoopField ; Array1_1 is Excel Row 1 column A, Array1_2 = Excel Row 1 column B
  } ; End-Loop Read through each Field in the ClipBoard data from Excel
} ; End-Loop Read through each Line (record) in the ClipBoard data from Excel

如果您需要更多示例来帮助您,请告诉我们 好了这里有更多的例子,您可以通过查看其他答案中的示例来清理它,例如使用Variable ++而不是Variable + = 1

; Read data from text file into one dimentional Array
ArrayCount = 0
Loop, Read, %A_ScriptDir%\SearchTerms.txt ; This loop retrieves each line from the file, one at a time.
{
    ArrayCount += 1  ; Keep track of how many items are in the array.
    Array%ArrayCount% := A_LoopReadLine ; Store this line in the next array element.
}


TextCounter = 1
IniWrite, %TextCounter%, C:\Temp\SearchTerms.ini, Counter, Nr ; Write counter in ini so I can restart at latest counter

SearchText:= Array1 ; Put data from Array1 into variable SearchText
MouseClick, left
Send, %SearchText%{Enter}
SplashTextOn, 200, 20,Searchterm,F%TextCounter% %SearchText%
WinMove, Searchterm, , , 0
Return

Browser_Favorites::
IniRead, TextCounter, C:\Temp\SearchTerms.ini, Counter, Nr ; Read latest counter
TextCounter += 1
IniWrite, %TextCounter%, C:\Temp\SearchTerms.ini, Counter, Nr
SearchText:=Array%TextCounter% ; Put data from Array+number into variable SearchText

; Examples with Array of 2 deep.

MyClipBoard=%ClipBoard% ; Remove any non-text (format) data

Loop, parse, MyClipBoard, `n,`r ; Place Excel content from clipboard in 2 dimentional Array
{ ; Start-Loop Read through each Line (record) in the ClipBoard data from Excel
  FullRecord:=A_LoopField ; Put a whole row in the variable FullRecord
  RecordNumber:=A_Index ; Store the current record (row) number.
  Loop, parse, FullRecord, %A_Tab% ; Parse the row content (FullRecord) in separate fields
  { ; Start-Loop Read through each Field in the ClipBoard data from Excel
    FieldNumber:=A_Index ; Store the current Field number.
    Array%RecordNumber%_%FieldNumber% := A_LoopField ; Array1_1 is Excel Row 1 column A, Array1_2 = Excel Row 1 column B
  } ; End-Loop Read through each Field in the ClipBoard data from Excel
} ; End-Loop Read through each Line (record) in the ClipBoard data from Excel



DLCounter:=1
DLSub:=1
DLData:=Array%DLCounter%_1
While (DLData <> "")
{
    While (DLData <> "")
    {
        MsgBox, %DLData%
        DLSub += 1
        DLData:=Array%DLCounter%_%DLSub%
    }
    DLSub:=1
    DLCounter += 1
    DLData:=Array%DLCounter%_%DLSub%
}
Return

它可能不是最干净的代码,但它会让你知道你能做什么。

答案 1 :(得分:0)

Autohotkey原样没有数组,但您可以模拟如下:

global counterArray := 1
localCounter := counterArray
YourArray%localCounter %:= your_value

向阵列添加新元素时;

counterArray++
anotherLocalCounter := counterArray
YourArray%localCounter%:= your_value

您也可以将此代码放入一个函数中并自行递增

ArrayExpand()
{
counterArray++
counter :=  counterArray
YourArray%counter %:=
}

重要提示:YourArray不能是全局的,并且YourArray%counter%中的计数器不能是全局的,其余的并不重要。