0
votes

I am looking to build an array of mouse click coordinates. I'm struggling to understand how you build out a two-dimensional array. Or if I'm going about this wrong and should build two arrays. One with X-coordinates and one with Y-coordinates.

I can easily build out an array with just X coordinates or just Y-coordinates. Can I somehow build out X and Y into the same 2-D array?

ArrayX := []
ArrayY := []
counter:=1

^Lbutton::
  MouseGetPos CurX, CurY
  ArrayX[counter] := CurX
  ArrayY[counter] := CurY
  counter++
return

^q::
  loop % ArrayX.MaxIndex()
    items.= ArrayX[A_Index] "," 
  StringLeft, items, items, Strlen(items)-1
  Clipboard := items
  msgbox % items
  items:=""  
  
  loop % ArrayY.MaxIndex()
    items.= ArrayY[A_Index] "," 
  StringLeft, items, items, Strlen(items)-1
  Clipboard := items
  msgbox % items
exitapp

1

1 Answers

3
votes

You'd create a multidimensional or a jagged array in AHK by just assigning the element at the desired index an array.
Array[3] := [481, 529]
And you'd access it by just accessing the elements in order, e.g:
Array[3][2] would result in 529.

Other things to improve on your script:

  1. You might want to use the .Push() method to append values to your array.
  2. A for loop might be a desirable alternative to normal looping and worrying about the index:
    for each, element in Array
  3. StringLeft is a deprecated legacy command and shouldn't be used anyone. SubStr() replaces it.
    However, for what you were doing with it, RTrim() would be better/easier:
    RTrim(items, ",")

Revised script:

Array := []

^LButton::
  MouseGetPos, CurX, CurY
  Array.Push([CurX, CurY])
return

^q::
    For each, element in Array
        items .= element[1] ", " element[2] "`n"
    MsgBox, % RTrim(items, "`n")
    ExitApp
return