Is there a way to enumerate the key-value pairs from an associative array in the order in which they're written or introduced by .push(), etc.? Are they actually stored in alphabetical order (since they're usually enumerated that way)?
If this can't be done, I'll probably have to resort to two parallel arrays, keys=[] and values=[] and add key value pairs to them at the same numerical index.
Can't enumerate associative array in order created? Topic is solved
Re: Can't enumerate associative array in order created? Topic is solved
You can make one of the values in the associative array an index number that would indicate the order in which they were created.
Re: Can't enumerate associative array in order created?
A scripting dictionary COM object can store the keys in the order of creation, iirc.
Please compare viewtopic.php?f=6&t=77&p=396#p396 (see second example)
Please compare viewtopic.php?f=6&t=77&p=396#p396 (see second example)
Re: Can't enumerate associative array in order created?
There is this class which allows you to keep your associative arrays in order: viewtopic.php?f=6&t=37083
Re: Can't enumerate associative array in order created?
Yes, I meant when I'm using an associative array because its keys are not sequential numbers, although that is the point of an associative array.
Thanks for your replies, guys. I don't think I'll bother trying to get my head around those other objects/classes. I struggle with all this object-oriented stuff. For my purpose, it will be ok to use two separate arrays and just address them in parallel, one holding the "key" (now a value, of course) and the other the value. I can then just push each item to its relevant linear array, or use keyarray[index] := key, valarray[index] := value for sparse arrays.
Re: Can't enumerate associative array in order created?
Yes, I know. My suggestion was for an associative array, as I mentioned. With an associative array, you could have each element contain an associative array with one of its key/values being your desired value (and more if you wanted) as well as a key/value that is the index number.
So instead of this, which shows them in alphabetical order:
Code: Select all
CarColors := {}
CarColors["Toyota"] := "red"
CarColors["BMW"] := "gray"
CarColors["Acura"] := "blue"
for Car, Color in CarColors
Out .= Car ":`t" Color "`n"
MsgBox, % Out
...you could do this which shows them in the order they were created:
Code: Select all
Cars := {}
Cars["Toyota"] := {color: "red", index: 1}
Cars["BMW"] := {color: "gray", index: 2}
Cars["Acura"] := {color: "blue", index: 3}
loop, % Cars.Count() {
OuterIndex := A_Index
for Car, Value in Cars
if (Value.index = OuterIndex)
Out .= Car ":`t" Value.color "`n"
}
MsgBox, % Out
This also allows you to add other key/value pairs if you want:
Code: Select all
Cars := {}
Cars["Toyota"] := {color: "red", style: "sedan", index: 1}
Cars["BMW"] := {color: "gray", style: "coupe", index: 2}
Cars["Acura"] := {color: "blue", style: "convertible", index: 3}
loop, % Cars.Count() {
OuterIndex := A_Index
for Car, Value in Cars
if (Value.index = OuterIndex)
Out .= Car ":`t" Value.color "`t" Value.style "`n"
}
MsgBox, % Out
Re: Can't enumerate associative array in order created?
Oh, I see - sorry I misunderstood. That's interesting and would be very powerful. In my case, I don't think I'd need those further pairs, so in keeping with the KISS principle, two parallel linear arrays might be easier to deal with. I suppose one method might have better (smaller) memory handling properties, but the data isn't enormous so it should be fine either way. I'll bear your method in mind, though, and put your reply as the solution to the thread. Thanks.boiler wrote: ↑12 Feb 2022, 06:20Yes, I know. My suggestion was for an associative array, as I mentioned. With an associative array, you could have each element contain an associative array with one of its key/values being your desired value (and more if you wanted) as well as a key/value that is the index number.
<snip>
...Indeed, you said, "one of the values".
Re: Can't enumerate associative array in order created?
This doesn't respond to the question but it can be a workaround
Code: Select all
Cars := [ { "Toyota" : { color : "red" , style : "sedan" } }
, { "BMW" : { color : "gray" , style : "coupe" } }
, { "Acura" : { color : "blue" , style : "convertible" } }
, { "Mercedes" : { color : "black" , style : "smooth" } } ]
Out := ""
For Each, One in Cars {
For Model, Desc in One {
Out .= Model ":`t " Desc.color "`t" Desc.style "`n"
}
}
Msgbox % Out
Re: Can't enumerate associative array in order created?
Yes, perfectly good 'workaround' - very similar to the above, but the index is taken care of by defining Cars as a linear array.Smile_ wrote: ↑12 Feb 2022, 08:13This doesn't respond to the question but it can be a workaroundCode: Select all
Cars := [ { "Toyota" : { color : "red" , style : "sedan" } } , { "BMW" : { color : "gray" , style : "coupe" } } , { "Acura" : { color : "blue" , style : "convertible" } } , { "Mercedes" : { color : "black" , style : "smooth" } } ] Out := "" For Each, One in Cars { For Model, Desc in One { Out .= Model ":`t " Desc.color "`t" Desc.style "`n" } } Msgbox % Out
I still prefer the simplicity (for my needs) of:
Code: Select all
key := [], val := []
key[1] := "zebra", val[1] := 25
key[2] := "terapin", val[2] := 15
key[5] := "cat", val[5] := 44
out =
loop, % key.length()
{
if key[A_Index] ; removing this also indicates blank elements in list
out .= key[A_Index] "`t" val[A_Index] "`n"
}
MsgBox % "There are " key.count() " items:`n" out