1
votes

I'm new to AutoHotKey and not familiar with its grammar. I've searched on the web but can't find usefully example.

Take a look at the following diagram.

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


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


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

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

I want to record some strings and navigate in them. For example, an array my_array was created when the program starts. If the user pressed ctrl+c, then the selected text (let's say it's string a) was copied into clipboard and also it was appended to my_array. If the user pressed another ctrl+c, then b was copied into clipboard and also it was appended to my_array. See (1), now a, b, c, d was appended to my_array and d is in clipboard. Now if the user press alt+left_arrow, then c was copied to clipboard, see (2). Now if the user pressed alt+left_arrow 2 times, a is in clipboard, see (3). The user can press alt+right_arrow 3 times to get back d in its clipboard, see (4). At this time, the user can still press ctrl+c to appended data to my_array and press alt+left_arrow or alt+right_arrow to move around in the array to get data back.

Actually to me this is easy to implement in some other familiar languages, but I have difficult to implement it in AutoHotKey. Anyone can help?

Thanks in advance.

2
AHK added support for proper arrays a few years ago. See AHK's documentation on arrays. I also recommend the SO Documentation page.Stevoisiak

2 Answers

1
votes

Here is an example of putting copied Excel data into an array.

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

If you need more examples to get you going, let me know.
OK here are some more examples, you could clean this up by looking at the examples in the other answers like using Variable++ instead of 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

It might not be the cleanest code, but it will give you an idea what you can do.

0
votes

Autohotkey as is does not have arrays, but you can simulate one like this:

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

When you add a new element to the array;

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

You can also put this code into a function and it increments itself

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

Important: YourArray must not be global and the counter in YourArray%counter% must not be global, the rest doesn't matter.