Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send the value of an array variable in Autohotkey?

I'm writing an AutoHotkey script which needs to display the value of an array variable, but it doesn't seem to be working properly.

MyArray := ["one", "two", "three"]

send MyArray[1]     ; "MyArray[1]"
send MyArray%1%     ; "MyArray"
send %MyArray1%     ; <empty>
send % MyArray%1%   ; <empty>
;send %MyArray%1%   ; 'Error: Variable name missing its ending percent sign'
;send %MyArray%1%%  ; 'Error: Empty variable reference (%%)'
;send %MyArray[1]%  ; 'Error: Variable name contains an illegal character'

I've found posts on the AHK forums claiming I can use send %MyArray1% or send % MyArray %1%, but both commands just reference empty variables.

How do I send an array's value in an AutoHotkey script?

like image 718
Stevoisiak Avatar asked Aug 11 '17 14:08

Stevoisiak


People also ask

How do you assign an array value to a variable?

Assigning values to associative array variable elements can be done by using the assignment statement in which the array is named, the index value is specified and the corresponding element value is assigned.

How do you give an element to an array?

Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory to hold the array, using new, and assign it to the array variable. Thus, in Java, all arrays are dynamically allocated.


1 Answers

Use a single % to force expression mode for a single parameter.

MyArray := ["one", "two", "three"]  ; initialize array
Send, % MyArray[1]                  ; send "one"

This can be combined with regular text using quotation marks ""

Send, % "The first value in my array is " MyArray[1]

Expression mode can be used with any command, including MsgBox and TrayTip.

MsgBox, % MyArray[1]
Traytip, , % "The first value is " MyArray[1]

See also:

  • AutoHotkey - Variables and Expressions
  • When are single percent % signs used in the script?
like image 178
Stevoisiak Avatar answered Sep 22 '22 17:09

Stevoisiak