Stevoisiak
Stevoisiak

Reputation: 26792

How do I print an array in AutoHotkey?

I have a set of arrays in AutoHotkey which I want to display as strings for debugging.

strArray := ["Alice", "Mary", "Bob"] ; 1D array
strArray2D := [[1,2,3], [4,5,6]]     ; 2D array

In Java, Javascript, and AutoIt I can accomplish this with the built-in toString() functions.

strArray.toString();            // JavaScript: "Alice,Mary,Bob"
Arrays.toString(strArray);      // Java:       "[Alice, Mary, Bob]"
_ArrayToString($strArray, ", ")  ; AutoIt:     "Alice, Mary, Bob"

AHK's developer lexikos has stated a built-in function for displaying arrays will not be added anytime soon, and most of the solutions I've found online seem fairly complex.

How can I print an array in AutoHotkey?

Upvotes: 7

Views: 7485

Answers (1)

Jim U
Jim U

Reputation: 3366

This converts 1 and 2 dimension arrays to strings

F2:: MsgBox % join(["Alice", "Mary", "Bob"])
F3:: MsgBox % join2D([[1,2,3], [4,5,6]])


join( strArray )
{
  s := ""
  for i,v in strArray
    s .= ", " . v
  return substr(s, 3)
}

join2D( strArray2D )
{
  s := ""
  for i,array in strArray2D
    s .= ", [" . join(array) . "]"
  return substr(s, 3)
}

enter image description here

Upvotes: 8

Related Questions