Arrays as Tables
1-based indexing in Lua.
Tables Are Lua's Lists
Lua has one container type: the table. When you store values in order, the table acts like an array or list.
You create an empty table with {} and fill it later, or list values right away inside the braces.
local fruits = {"apple", "banana", "cherry"}
print(fruits)Indexing Starts at 1
This is the most important rule: Lua arrays are 1-based. The first item lives at index 1, not 0.
Use square brackets to read an element by its position.
local fruits = {"apple", "banana", "cherry"}
print(fruits[1])
print(fruits[2])
print(fruits[3])All lessons in this course
- Arrays as Tables
- Adding and Removing Items
- Iterating with ipairs
- Length and Sorting