7

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?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • This one https://autohotkey.com/board/topic/70490-print-array/?p=492728 from the link you posted is quite simple and can print arrays with any depth. – Oleg Sep 03 '17 at 16:53

1 Answers1

7

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

Jim U
  • 3,318
  • 1
  • 14
  • 24