Is it possible to not reorder the key values of the output associative array when using a For loop? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
afe
Posts: 599
Joined: 06 Dec 2018, 04:36

Is it possible to not reorder the key values of the output associative array when using a For loop?

10 Jul 2019, 02:56

Hello,

When a For loop is used to traverse an associative array, it sorts the keys and outputs them. Is it possible to output in the default order without sorting?

For example, in the example below, 30 will output before 20,

ten=10 thirty=30 twenty=20

I expect the output order to be

ten=10 twenty=20 thirty=30

Thanks.

Code: Select all

array := {ten: 10, twenty: 20, thirty: 30}
For key, value in array
    MsgBox %key% = %value%
just me
Posts: 7175
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?  Topic is solved

10 Jul 2019, 03:09

The built-in For-Loop does not reorder nor sort the keys. They are stored in ascending order by default.
afe
Posts: 599
Joined: 06 Dec 2018, 04:36

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

10 Jul 2019, 03:25

What do you mean when data is reordered when it is saved to an associative array, is that true?
Last edited by afe on 10 Jul 2019, 03:28, edited 2 times in total.
Helgef
Posts: 4450
Joined: 17 Jul 2016, 01:02
Contact:

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

10 Jul 2019, 03:28

The order of keys in an associative arrays are not documented. You can use a linear array of data pairs, example,

Code: Select all

array := [ ["ten", 10], ["twenty", 20], ["thirty", 30] ]
For key, pair in array
    MsgBox % pair[1] . " = " pair[2]
Cheers.
afe
Posts: 599
Joined: 06 Dec 2018, 04:36

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

10 Jul 2019, 03:39

Helgef wrote:
10 Jul 2019, 03:28
The order of keys in an associative arrays are not documented. You can use a linear array of data pairs, example,

Code: Select all

array := [ ["ten", 10], ["twenty", 20], ["thirty", 30] ]
For key, pair in array
    MsgBox % pair[1] . " = " pair[2]
Cheers.
I have not found this usage in the manual, but it works. However, I need to use an associative array to save the key values to a file.
Occasionally found the reordering of associative arrays, but this does not affect use.
just me
Posts: 7175
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

10 Jul 2019, 04:12

Which enumeration order would you expect for this associative array?

Code: Select all

array := {30: "thirty", 20: "twenty", 10: "ten"}
For key, value in array
    MsgBox %key% = %value%
The order of keys in associative arrays is not documented. But they are stored in ascending order grouped as integer keys, string keys, and object keys. It's very unlikely that this will be changed in v1.1.
afe
Posts: 599
Joined: 06 Dec 2018, 04:36

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

10 Jul 2019, 05:31

I expect to keep the default order,

30=thirty
20=twenty
10=ten

Ok, this is not a problem.
User avatar
Micromegas
Posts: 219
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

25 May 2020, 18:28

Sorry for replying to last years post. For me, this is a problem. If there was a way to remember the order of the definition, it would be possible to use the following generic function:

Code: Select all

aa1 := {begin: 100, middle: 200, end: 300}
aa2 := {top: 0, center: 10, bottom: 20}

msgbox % ToString(aa1)
msgbox % ToString(aa2)

ToString(aa) {
	r := ""
	For key, value in aa
		r .= Format("{}:`t{}`n", key, value)
	return r
}
This function obfuscates its information because it doesn't know the key order. Or is there any practical way to define such a ToString function to be meaningful?
User avatar
boiler
Posts: 5668
Joined: 21 Dec 2014, 02:44

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

25 May 2020, 19:14

Can you use Helgef's suggested method of using a linear array of data pairs? :

Code: Select all

aa1 := [ ["begin", 100], ["middle", 200], ["end", 300] ]
aa2 := [ ["top", 0], ["center", 10], ["bottom", 20] ]

msgbox % ToString(aa1)
msgbox % ToString(aa2)

ToString(aa) {
	r := ""
	For key, value in aa
		r .= Format("{}:`t{}`n", value.1, value.2)
	return r
}
User avatar
Micromegas
Posts: 219
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Is it possible to not reorder the key values of the output associative array when using a For loop?

26 May 2020, 02:15

Thank you for the suggestion. Yes, that does deliver the output I want – but at the expense of mangling the data structure. The reason I called the variables “aa...” was that I have associate arrays given, and I just needed a way to display them nicely.

But I found a workaround that keeps the data structure intact. It even has the nice side effect that the syntax coloring now doesn't take some of the keywords, such as “top” and “end”, as reserved keywords anymore. (At least in NP++ – this bulletin board' s heuristic is more discriminating.)

Code: Select all

aa1 := {1begin: 100, 2middle: 200, 3end: 300}
aa2 := {05top: 0, 10center: 10, 15bottom: 20}

msgbox % ToString(aa1)
msgbox % ToString(aa2)

ToString(aa, omit_initial_numbers := True) {
	r := ""
	For key, value in aa {
		if omit_initial_numbers
			key := RegExReplace(key,"^\d*")
		r .= Format("{}:`t{}`n", key, value)
	}
	return r
}

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, boiler, pnKed, SilasDeVis and 49 guests