User:OrenBochman/Lua Tables
The table is Lua's primary data structure
Creating a table
editnumbers = {
one = 1,
two = 2,
three = 3
}
Accessing a table element
editreturn numbers.one -- returns 1
return numbers['one'] -- also returns 1
Numbered tables
editafricanFlatbreads = {
'Aish Mehahra',
'Injera',
'Lahoh',
'Ngome'
}
return africanFlatbreads[2] -- returns 'Injera'
Visiting each table element
edit- 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