User:OrenBochman/Lua Tables

The table is Lua's primary data structure

Creating a tableEdit

numbers = {
   one = 1,
   two = 2,
   three = 3
}

Accessing a table elementEdit

return numbers.one -- returns 1


return numbers['one'] -- also returns 1

Numbered tablesEdit

africanFlatbreads = {
    'Aish Mehahra',
    'Injera',
    'Lahoh',
    'Ngome'
}

return africanFlatbreads[2] -- returns 'Injera'


Visiting each table elementEdit

  • pairs: key/value pairs in random order
for name, number in pairs(numbers) do
...
end
  • ipairs: Numeric keys in ascending order
for index, bread in ipairs(africanFlatbreads) do
...
end