For loop in multidimensional array at certain dimension

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rokorps
Posts: 9
Joined: 13 May 2016, 15:08

For loop in multidimensional array at certain dimension

Post by rokorps » 11 Aug 2022, 04:14

Hello,
I have been working with a multidimensional arrays, and tried to search for a feature to loop only thorugh certain dimension of array, while other its dimensions are static.
I have found only how to loop through dimensions in order.
For example I have array:

Code: Select all

la:=[]
la[1,1,1]:=111
la[1,1,2]:=112
la[1,2,1]:=121
la[1,2,2]:=122
la[2,1,1]:=211
la[2,1,2]:=212
la[2,2,1]:=221
la[2,2,2]:=222
So I found a way only to loop through its last dimension i.e:

Code: Select all

for key, value in la[1,1]
But what if i want to loop in its x dimension, while others keeping constant (la[1,x,1]), without creating additional array?

Regards,
Rokas

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: For loop in multidimensional array at certain dimension

Post by boiler » 11 Aug 2022, 06:04

rokorps wrote: But what if i want to loop in its x dimension, while others keeping constant (la[1,x,1]), without creating additional array?
You can enumerate the whole object and act when the keys equal what you specified:

Code: Select all

la:=[]
la[1,1,1]:=111
la[1,1,2]:=112
la[1,2,1]:=121
la[1,2,2]:=122
la[2,1,1]:=211
la[2,1,2]:=212
la[2,2,1]:=221
la[2,2,2]:=222

for k1, v1 in la
	for k2, v2 in v1
		for k3, v3 in v2
			if (k1 = 1) && (k3 = 1)
				MsgBox, % k2 ": " v3

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: For loop in multidimensional array at certain dimension

Post by lexikos » 11 Aug 2022, 22:26

It is not a single array. When you use multiple indices in an assignment (with a single pair of square brackets in AutoHotkey v1.1), the object creates additional arrays as needed. Once a value is assigned to la[1,1,1], la[1] and la[1][1] are both arrays of arrays, and la[1][1][1] contains the value.

If that is the only value assigned, for key, value in la will have one iteration, with value being the array contained by la[1].

If you want two of the dimensions to remain constant, there is no need to enumerate those dimensions. for key, arr in la[1] will enumerate the arrays of (for instance) la[1, 1..2]. You can then use arr[1] to access the value.

Code: Select all

la:=[]
la[1,1,1]:=111
la[1,1,2]:=112
la[1,2,1]:=121
la[1,2,2]:=122
la[2,1,1]:=211
la[2,1,2]:=212
la[2,2,1]:=221
la[2,2,2]:=222

for k, arr in la[1]
	MsgBox % arr[1]
Also, Wish List is for feature requests. I have moved the topic to Ask for Help.

rokorps
Posts: 9
Joined: 13 May 2016, 15:08

Re: For loop in multidimensional array at certain dimension

Post by rokorps » 18 Aug 2022, 07:27

I see, when there is iteration from left to right we provide previous values in loop definement, and when there is dimensions left on the right side, we just provide their values in iterated array definition. Nice
Danke,
Rokas

Post Reply

Return to “Ask for Help (v1)”