[FIXED] pairs() does not iterate through the dictionary in order

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
pyxledev
Prole
Posts: 27
Joined: Tue Jul 26, 2022 2:19 pm
Contact:

[FIXED] pairs() does not iterate through the dictionary in order

Post 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.
Last edited by pyxledev on Sun Nov 06, 2022 4:08 pm, edited 1 time in total.
User avatar
GVovkiv
Party member
Posts: 688
Joined: Fri Jan 15, 2021 7:29 am

Re: pairs() does not iterate through the dictionary in order

Post 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
User avatar
BrotSagtMist
Party member
Posts: 665
Joined: Fri Aug 06, 2021 10:30 pm

Re: pairs() does not iterate through the dictionary in order

Post 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.
obey
User avatar
pyxledev
Prole
Posts: 27
Joined: Tue Jul 26, 2022 2:19 pm
Contact:

Re: pairs() does not iterate through the dictionary in order

Post 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.
User avatar
pyxledev
Prole
Posts: 27
Joined: Tue Jul 26, 2022 2:19 pm
Contact:

Re: pairs() does not iterate through the dictionary in order

Post 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!
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests