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
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
Code: Select all
For colorNum, color in ["yellow", "green", "blue", "red"]
list .= color ":" colorNum A_Tab
MsgBox, 64, List, %list%
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:PuzzledGreatly wrote: ↑ I thought the loop indicated the order.
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
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
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%