Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

i,v in pairs wont iterate through a table in order when I assigned them keys?

Asked by 4 years ago
local Tab = {Xd = 'G', ge  = 'g',ce = 'gg',keke = '1', fefe = '2'}

for i,v in pairs(Tab) do
    print(i,v)
end

will not print in order but when i remove the keys like Xd, ge, ce, keke, fefe it itereates in order cna anyone tell me why it dosen't iteate in order and i can get it to iterate in order

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
4 years ago

pairs does not iterate in any particular order -- the keys of a table do not have an order; they are merely present.

If your data has an order, you should organize your table with that order as a sequence. Maybe something like this:

local data = {
    {"Xd", "g"},
    {"ge", "g"},
    {"ce", "gg"},
    {"keke", "1"},
    {"fefe", "2"},
}

-- note that "ipairs" and not "pairs" is used --
-- ipairs goes in order, but only over *lists*
for _, kv in ipairs(data) do
    print(kv[1], kv[2])
end
Ad

Answer this question