Order of indices in for k, v in array-style loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Order of indices in for k, v in array-style loop

Post by wpb » 26 Jan 2023, 06:46

Hi there,
I've checked the docs, but I can't find a definitive answer. So...
In an array with purely integer keys, is a

Code: Select all

for index, value in array
loop guaranteed to iterate over those indices in ascending order?
Thanks.
(I'd like to know for both v1 and v2 of AHK, if someone can enlighten me.)

User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Order of indices in for k, v in array-style loop

Post by mikeyww » 26 Jan 2023, 08:32

I know a way to find out.

Code: Select all

#Requires AutoHotkey v1.1.33
arr1 := { 1 : "A",  100 : "B",  1000 : "C",  20 : "D"}
arr2 := {"1": "A", "100": "B", "1000": "C", "20": "D"}
For each, arr in [arr1, arr2] {
 For k, v in arr
  Send % v
 Send `n
}

Code: Select all

#Requires AutoHotkey v2.0
arr1 := Map( 1 , "A",  100 , "B",  1000 , "C",  20 , "D")
arr2 := Map("1", "A", "100", "B", "1000", "C", "20", "D")
For each, arr in [arr1, arr2] {
 For k, v in arr
  Send v
 Send '`n'
}

wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Re: Order of indices in for k, v in array-style loop

Post by wpb » 26 Jan 2023, 09:57

Thanks for the code. I do appreciate you taking the time to provide that. But what I'd really like to know is: is that /guaranteed/ by the language spec. In other words, can I rely on it even in future versions?

just me
Posts: 9451
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Order of indices in for k, v in array-style loop

Post by just me » 26 Jan 2023, 10:09

'Plain arrays' with purely integer keys (v1.1) and Array objects (v2) are iterated in ascending order. Currently it's /guaranteed/ by the language spec, but who knows? ;)

wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Re: Order of indices in for k, v in array-style loop

Post by wpb » 26 Jan 2023, 10:13

If it's in the documentation that it's guaranteed, then I'd expect it to remain guaranteed (even in v2, since that's now the official version). Can you point me to where in the documentation it says so? Many thanks!

User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Order of indices in for k, v in array-style loop

Post by mikeyww » 26 Jan 2023, 12:03

I did not see it in the docs, but it is known for simple arrays, which always have numeric keys.

wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Re: Order of indices in for k, v in array-style loop

Post by wpb » 26 Jan 2023, 16:41

Okay, thanks!

User avatar
DrReflex
Posts: 42
Joined: 25 May 2015, 02:57
Location: Greer, SC

Re: Order of indices in for k, v in array-style loop

Post by DrReflex » 12 Feb 2023, 09:43

Great example. I like this modification even better. Both show how in arr1 the keys are numeric and are accessed sequentially in numerical order (A,D,C,B) and in arr2 the keys are strings and are accessed sequentially in alphabetically sorted order (E,G,F,H)

Code: Select all

#Requires AutoHotkey >=2.0-<2.1
#SingleInstance Force

arr1 := Map( 1 , "A",  1000 , "B",  100 , "C",  20 , "D")
arr2 := Map("1", "E", "1000", "F", "100", "G", "20", "H")
For each, arr in [arr1, arr2] {
 For key, var in arr
  MsgBox var
}

Esc:: {
	ExitApp()
}

Post Reply

Return to “Ask for Help (v1)”