lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 18/10/2011 8.09, steve donovan wrote:
To get the order you want, one way is to keep a sub-table of the keys
in order, and use that for iteration.

Or, assuming numbers here are arbitrary and not just consecutive, if you need a table of pairs you could store them as subtables:

-- elements with no key are stored in order (table as array)
t = {
  {'x', 1},
  {'y', 2},
  {'a', 3},
  {'b', 4}
}

-- read them back in the same order (note 'ipairs')
for _, s in ipairs(t) do
  print(s[1], s[2])
end

--> output:
x       1
y       2
a       3
b       4

--
  Enrico