Page 1 of 1
[FIXED] pairs() does not iterate through the dictionary in order
Posted: Sun Nov 06, 2022 10:14 am
by pyxledev
Hey everybody, I've been trying to iterate over a stats dictionary but it always ends up giving it mixed up. Here is some code and output:
Code: Select all
Stats = {
waves = 0;
time = 0;
accuracy = 0;
kills = 0;
dashKills = 0;
}
Code: Select all
for i in pairs(Stats) do
print(i)
...
end
Output:
Code: Select all
zerpnord@kubuntu:~/Belgeler/Projeler/blockdash$ love .
dashKills
waves
time
kills
accuracy
As you can clearly see, it's not in order which causes some issues. Does anybody know how to fix this? Preferably without a custom function, thanks.
Re: pairs() does not iterate through the dictionary in order
Posted: Sun Nov 06, 2022 12:06 pm
by GVovkiv
pyxledev wrote: ↑Sun Nov 06, 2022 10:14 am
Hey everybody, I've been trying to iterate over a stats dictionary but it always ends up giving it mixed up. Here is some code and output:
Code: Select all
Stats = {
waves = 0;
time = 0;
accuracy = 0;
kills = 0;
dashKills = 0;
}
Code: Select all
for i in pairs(Stats) do
print(i)
...
end
Output:
Code: Select all
zerpnord@kubuntu:~/Belgeler/Projeler/blockdash$ love .
dashKills
waves
time
kills
accuracy
As you can clearly see, it's not in order which causes some issues. Does anybody know how to fix this? Preferably without a custom function, thanks.
lua's non numeric values in tables stored as hash, which you can't really sort in any specific way (at least, not in sane way)
you better off use numeric keys and sort them with table.sort in specified way
Re: pairs() does not iterate through the dictionary in order
Posted: Sun Nov 06, 2022 1:12 pm
by BrotSagtMist
Jup, having no order is in the very definition of this tables. Its not really useful anyway.
You could do a secondary table system to combine strings and integers in ordered mode and of course use ipairs:
Code: Select all
Stats1 = {
"waves";
"time";
"accuracy";
}
Stats2 = {0,0,0}
for k,v in ipairs(Stat1)
print(v,Stats2[k])
end
But usually anything that needs to display values should be aware of what it is supposed to print, therefore, this is not really a real world usecase.
You do print(Stat.health) and not print(Stat whatever is at position 4) in game UIs.
Re: pairs() does not iterate through the dictionary in order
Posted: Sun Nov 06, 2022 4:05 pm
by pyxledev
GVovkiv wrote: ↑Sun Nov 06, 2022 12:06 pm
pyxledev wrote: ↑Sun Nov 06, 2022 10:14 am
Hey everybody, I've been trying to iterate over a stats dictionary but it always ends up giving it mixed up. Here is some code and output:
Code: Select all
Stats = {
waves = 0;
time = 0;
accuracy = 0;
kills = 0;
dashKills = 0;
}
Code: Select all
for i in pairs(Stats) do
print(i)
...
end
Output:
Code: Select all
zerpnord@kubuntu:~/Belgeler/Projeler/blockdash$ love .
dashKills
waves
time
kills
accuracy
As you can clearly see, it's not in order which causes some issues. Does anybody know how to fix this? Preferably without a custom function, thanks.
lua's non numeric values in tables stored as hash, which you can't really sort in any specific way (at least, not in sane way)
you better off use numeric keys and sort them with table.sort in specified way
Thanks for the explanation, I'll change the code.
Re: pairs() does not iterate through the dictionary in order
Posted: Sun Nov 06, 2022 4:07 pm
by pyxledev
BrotSagtMist wrote: ↑Sun Nov 06, 2022 1:12 pm
Jup, having no order is in the very definition of this tables. Its not really useful anyway.
You could do a secondary table system to combine strings and integers in ordered mode and of course use ipairs:
Code: Select all
Stats1 = {
"waves";
"time";
"accuracy";
}
Stats2 = {0,0,0}
for k,v in ipairs(Stat1)
print(v,Stats2[k])
end
But usually anything that needs to display values should be aware of what it is supposed to print, therefore, this is not really a real world usecase.
You do print(Stat.health) and not print(Stat whatever is at position 4) in game UIs.
I actually have another table called StatNames used for the display names in UI. I also tried to loop over StatNames & get Stats
because they are in the same order, but it returned nil. It turns out there is no indexing in dicts, so I'll probably straight up write values in Stats just like you wrote. Thanks for the help!