SafeArray - enumerate dimension with for-loop

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

SafeArray - enumerate dimension with for-loop

Post by kczx3 » 04 May 2021, 09:51

Is it possible to enumerate a single dimension of a SafeArray using a for-loop?

Code: Select all

; loop through items in first dimension
for item in mySafeArray[0] { ; results in error - "Invalid number of parameters"
    msgbox(item)
}
If I continue through the first error, I then get another error pointing to the same line stating "Value not enumerable" but that's probably because mySafeArray[0] results in an empty string or something.

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

Re: SafeArray - enumerate dimension with for-loop

Post by lexikos » 04 May 2021, 17:07

No.

Unless you redefine __Enum first (v2.0-a133+)...

On second thought, you don't need to redefine __Enum; you can create an enumerator function directly, and then pass it to the for-loop.

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: SafeArray - enumerate dimension with for-loop

Post by kczx3 » 05 May 2021, 13:04

Thanks for the reply. Appreciate it.

RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Re: SafeArray - enumerate dimension with for-loop

Post by RomAnT » 28 Sep 2021, 11:57

lexikos wrote:
04 May 2021, 17:07

On second thought, you don't need to redefine __Enum; you can create an enumerator function directly, and then pass it to the for-loop.
Can someone give a code example how this done?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: SafeArray - enumerate dimension with for-loop

Post by swagfag » 02 Oct 2021, 12:03

RomAnT wrote:
28 Sep 2021, 11:57
lexikos wrote:
04 May 2021, 17:07

On second thought, you don't need to redefine __Enum; you can create an enumerator function directly, and then pass it to the for-loop.
Can someone give a code example how this done?

Code: Select all

#Requires AutoHotkey v2.0-beta.1

mySafeArray := ComObjArray(VT_VARIANT:=12, 1, 3)
mySafeArray[0, 0] := "Auto"
mySafeArray[0, 1] := "Hot"
mySafeArray[0, 2] := "key"

; loop through items in first dimension
for item in (
	(secondDimIndex := 0) => ( ; zerobased indices
		(&key) => ( ; enumerator func
			(secondDimIndex <= mySafeArray.MaxIndex(2)) ; has items left?
			? (
				key := mySafeArray[0, secondDimIndex++], ; get current item, advance index
				true ; continue enumerating remaining items
			) 
			: (
				false ; index out of bounds, no items left, stop enumerating
			)
		)
	)
)() ; dont forget to call it
{
	msgbox(item)
}


Post Reply

Return to “Ask for Help (v2)”