Associative Array Order?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Associative Array Order?

Post by PuzzledGreatly » 04 Jul 2022, 18:18

Please look at the following code:

Code: Select all

seq := {}
col := ["yellow","green","blue","red"]

loop 4
{
	msgbox, 4096, col,% col[A_index]
	seq[col[A_index]] := A_index
}

test := ""
for k, v in seq
test .= k ":" v A_tab

msgbox, 4096, why?,% test
Can someone please explain why the test message is showing as "blue:3 green:2 red:4 yellow:1" rather than "yellow:1 green:2 blue:3 red:4". Thanks.

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

Re: Associative Array Order?

Post by boiler » 04 Jul 2022, 18:20

Because there is no order kept regarding when they are added. They are enumerated in an alphanumeric sort order by key.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Associative Array Order?

Post by PuzzledGreatly » 04 Jul 2022, 19:24

Thanks for the reply but I don't quite follow. I thought the loop indicated the order. How could the order be included? Thanks.

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

Re: Associative Array Order?

Post by mikeyww » 04 Jul 2022, 19:35

Code: Select all

For colorNum, color in ["yellow", "green", "blue", "red"]
 list .= color ":" colorNum A_Tab
MsgBox, 64, List, %list%
Also: Scripting.Dictionary

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

Re: Associative Array Order?

Post by boiler » 04 Jul 2022, 21:35

PuzzledGreatly wrote: I thought the loop indicated the order.
No, the loop that you may happen to use to add elements to an array has nothing to do with the order when the array is enumerated except perhaps sometimes by coincidence. That's even true with simple arrays. The reason they usually are enumerated in the same order they are added for a simple array is that they are usually added in sequential order. But that still has to do with the keys themselves, not the order they were added. As the script below shows, the order they're enumerated has only to do with the alphanumeric sort of the keys, just like with associative arrays. The order these are added has nothing to do with how they are reported back in the enumeration loop:

Code: Select all

MyArray := []
MyArray[3] := "Three"
MyArray[1] := "One"
MyArray[4] := "Four"
MyArray[2] := "Two"

for k, v in MyArray
	out .= k " : " v "`n"
MsgBox, % out
Output:
1 : One
2 : Two
3 : Three
4 : Four



You can order the output of an associative array based on the values of the elements instead of their keys like this (assuming sequential integer values like yours):

Code: Select all

seq := {}
col := ["yellow","green","blue","red"]

loop 4
{
	msgbox, 4096, col,% col[A_index]
	seq[col[A_index]] := A_index
}


; output in order of value instead of key:
loop, % seq.Count() {
	outerIndex := A_Index
	for k, v in seq
		if (v = outerIndex)
			out .= k " : " v "`n"
}
msgbox, % out
Output:
yellow : 1
green : 2
blue : 3
red : 4

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

Re: Associative Array Order?

Post by mikeyww » 05 Jul 2022, 04:47

Another one:

Code: Select all

col := ["yellow","green","blue","red"], arr := {}, seq := []
For colorNum, color in col
 arr[color] := colorNum
For color, colorNum in arr
 seq[colorNum] := color
For colorNum, color in seq
 text .= color ": " colorNum "`n"
MsgBox, 64, Result, %text%
image220705-0547-001.png
Output
image220705-0547-001.png (7.89 KiB) Viewed 519 times

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Associative Array Order?

Post by PuzzledGreatly » 24 Jul 2022, 17:48

Thanks for the replies. I've been thinking about them and found an answer to the problem I was having. I think I might be getting an idea now of how an associative array works. Thanks again.

Post Reply

Return to “Ask for Help (v1)”