Multidimensional array

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Multidimensional array

30 Jul 2021, 15:53

Can't wrap my head around a 2d array in v2, this works in v1, what would be the equivalent code in v2?

Code: Select all

c:=[]
c[1,1]:="test1"
c[1,2]:="test2"
MsgBox % c[1,2] ; test2 
I did see https://lexikos.github.io/v2/docs/Objects.htm#Usage_Arrays_of_Arrays but I don't know in advance how many rows/columns I have.
neogna2
Posts: 590
Joined: 15 Sep 2016, 15:44

Re: Multidimensional array

31 Jul 2021, 03:08

I've only recently started using v2 but this is what I have been doing.

Code: Select all

;v2 2D array
c := []
d := []
;d := Array() ;equivalent
d.Push("test1")
d.Push("test2")
c.Push(d)
MsgBox(c[1][2]) ;test2

;v2 2D array alternative
c := []
d := []
c.Length := 100 ;add some slack
d.Length := 100
d[1] := "test1"
d[2] := "test2"
c[1] := d
MsgBox(c[1][2]) ;test2
I would be happy if someone else shows a shorter way to do it. One feature request idea I had was if the .= operator could be made into an alias for Push if preceded by an Array in addition to that operator's normal use for variables. If that syntax existed we could do d .= "test1" instead of d.Push("test1")
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Multidimensional array

31 Jul 2021, 03:30

The documentation describes how c[1,1] is handled in v1.
https://www.autohotkey.com/docs/Objects.htm#Usage_Arrays_of_Arrays

It should be reasonably trivial to implement similar behaviour by (re)defining __Item. You may use Array or Map as a base, but it is not a requirement; any object can be used like c[1,1] if it defines an __Item property which accepts two (or a varying number of) parameters.

When c[1,1] := value (which is equivalent to c.__Item[1,1] := value) is evaluated, __Item would assign super[1] a new map/array/object if not already set (where super is used literally to refer to the base-class version of __Item, or is replaced with a reference to an internal array/map/object), and then assign super[1][1] := value.

If you use a plain Array directly, you just have to assign another Array to c[1] before assigning c[1][1]. (Perhaps avoid thinking of it is a multidimensional array; it is an array of arrays.)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Multidimensional array

24 Aug 2021, 08:08

I wrote multidim array and map for alpha, it shouldn't need much modification to work in beta.

See :arrow: [a108] N-dimensional array. and :arrow: [a108] N-dimensional map.

Cheers.
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Multidimensional array

26 Aug 2021, 15:27

This is above my skill level I'm afraid and I'll stick to [1][1] v [1,1] when/where needed
User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: Multidimensional array

26 Aug 2021, 15:43

@Helgef
I took a look at your code. Am I correct in concluding that your solution essentially puts/gets the result to/from a linear array?

And the index is the result of all dimensions multiplied together?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Multidimensional array

30 Aug 2021, 11:47

The n-dim array just access indices in an underlying linear array, yes. Indices arn't just mulitplied as then you couldn't distinct between arr[1,2] and arr[2,1], the formula is desrcibed in the comments I believe. The map works differently.

@ahk7, the documentation has a 2-D example :arrow: Arrays of arrays

Cheers.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: gkaoyan and 42 guests