I created a table, which lists 5 key value pairs. When I run my program the values are not in the same order they are listed when I set the variable!
For example:
local table = { [“A”]=2, [“B”]=5, [“C”]=8, [“D”]=11, [“E”]=12}
for k, v in pairs( table ) do
print(v .. " " .. k)
end
Displays:
2 A
8 C
5 B
12 E
11 D
Is this standard behavior?
[import]uid: 98652 topic_id: 17365 reply_id: 317365[/import]
yes that’s normal [import]uid: 7911 topic_id: 17365 reply_id: 65715[/import]
Just to be clear. You can’t predict the order of an array? [import]uid: 98652 topic_id: 17365 reply_id: 65719[/import]
no
but you can do
local table ={ { [“A”]=2},{[“B”]=5}, …}
then
for i,t in pairs(table) do
for k,v in pairs(t) do
print( i,k,v)
end
end [import]uid: 7911 topic_id: 17365 reply_id: 65720[/import]
Thanks for the reply. The nested table seems to work. I’m guessing this has something to do with the outer array now using an index rather than a property name? [import]uid: 98652 topic_id: 17365 reply_id: 65727[/import]
correct [import]uid: 7911 topic_id: 17365 reply_id: 65728[/import]