Lesson 11 of 15

Tables as Arrays

Tables as Arrays

Tables are the only data structure in Lua. When used with integer keys starting at 1, they act as arrays:

local fruits = {"apple", "banana", "cherry"}

print(#fruits)  -- 3 (length)

Important: Lua arrays are 1-indexed, not 0-indexed!

Modifying Tables

table.insert(fruits, "date")     -- appends "date"
table.remove(fruits, 1)          -- removes first element

Concatenation

table.concat joins array elements:

local parts = {"Hello", "World"}
print(table.concat(parts, " "))  -- "Hello World"

Your Task

Create an array of numbers, add an element, and print the count and concatenated result.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.