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?

1

1 Answer 1

8

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

2
  • Why does the function return a substring?
    – Stevoisiak
    Commented Sep 1, 2017 at 17:10
  • 1
    to trim a leading delimiter if present
    – Jim U
    Commented Sep 1, 2017 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.