Access next key/value pair in For ..in loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pramach
Posts: 54
Joined: 24 Jan 2018, 08:20

Access next key/value pair in For ..in loop

10 Dec 2022, 09:00

Hi

Assuming I have an associative array with n key/value pairs.

While looping through, I want to know the Key/value pair of the next element in the array.

Something like ...:

Code: Select all

for k, v in myArray
{
	if (k = "ABC")
		msgbox % myArrayNextKey
}
I tried with myArray.Next(k2,v2) but did not get any value inside k2 and v2.

I know I can use a flag, do Continue and check the flag on next loop.
But I think (or hope) there is a better way.

Any help welcome.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Access next key/value pair in For ..in loop

10 Dec 2022, 09:23

Code: Select all

arr := {a: "b", abc: "x", 9: "f", d: 44} ; 9, a, abc, d
MsgBox, 64, ABC, % next(arr, "ABC")
MsgBox, 64,   a, % next(arr, "a"  )
MsgBox, 64,   9, % next(arr, 9    )

next(arr, item) {
 keyList := [], index := []
 For k in arr
  keyList.Push(k), index[k] := A_Index
 Return keyList[index[item] + 1]
}
pramach
Posts: 54
Joined: 24 Jan 2018, 08:20

Re: Access next key/value pair in For ..in loop

10 Dec 2022, 09:59

thanks a lot-

So the solution is to put first all key's into a simple array in order to access than the simple array by index.

But, to be honest, does this makes sense for big arrays with several hundred or thousands of items ?
From a memory usage point of view I would say no :D

OK, it depends.
When I need the next key only once, my "flag-continue-check flag" proposal makes more sense.
If I need the value of the next item several times within my loop, your simple array solution is better.

Thanks anyway as I just learned something new.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Access next key/value pair in For ..in loop

10 Dec 2022, 10:01

I can't say that my approach is better, though it's different! :) I agree with your comments.

There could be a trick that accesses the memory directly, but it's beyond my knowledge. In terms of an associative array itself using the native AHK, so to speak, one has to loop to get to the next key.

If your array does not change, you can index it once at the start, instead of each time inside the function.

Code: Select all

arr  := {a: "b", abc: "x", 9: "f", d: 44}
next := {}
For k in arr ; 9, a, abc, d
 last := next[last] := k
For each, item in ["ABC", "a", 9]
 MsgBox, 64, %item%, % next[item]
pramach
Posts: 54
Joined: 24 Jan 2018, 08:20

Re: Access next key/value pair in For ..in loop

16 Dec 2022, 05:09

Sorry for late reply.

Creating the index outside was of sure my assumption :lol:
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Access next key/value pair in For ..in loop

16 Dec 2022, 09:36

Code: Select all

myArraysEnum := myArray._NewEnum()
while myArraysEnum.Next(k, v) 
{
	if (k = "ABC")
	{
		; get next element, if there was one
		if myArraysEnum.Next(myArrayNextKey, myArrayNextValue)
		{
			msgbox % myArrayNextKey
		}

		; unconditionally decrement mOffset back down once
		static offsetof_mOffset := 3 * A_PtrSize
		NumPut(NumGet(&myArraysEnum, offsetof_mOffset, "Int") - 1, &myArraysEnum, offsetof_mOffset, "Int")
	}
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Marium0505 and 346 guests