lua notes
lua

iterating tables

looping over a tables values with pairs, ipairs, or a plain numeric for

use pairs n throw the key away with _

for _, v in pairs(t) do
    print(v)
end

which loop

  • array style table ({ a, b, c }), use ipairs instead, it goes in order from 1 n stops at the first nil
  • pairs has no guaranteed order, it gives you everything, hash part included, but in whatever order the table happens to store it
  • a plain numeric loop is the fastest option for arrays
for i = 1, #t do
    local v = t[i]
    print(v)
end

On this page