Lua Fundamentals

Lesson 3 of 6

Tables: Lua's Only Data Structure

Lua has exactly one built-in data structure, the table, and it does the job of arrays, dictionaries/maps, and even objects in other languages.

Tables as arrays

Lua

ipairs iterates a table in order from index 1 until the first nil, exactly what you want for array-like tables.

Tables as maps/objects

Lua

pairs iterates every key in a table, in an unspecified order, use it for map-style tables where order doesn't matter. player.name and player["name"] always refer to the exact same table entry, dot syntax is purely a convenience for string keys that are valid identifiers.

TIP

Because arrays, maps, and objects are all "just a table" in Lua, a single table can even mix both styles: numeric indices for a list part and named keys for extra fields on the same object.

📝 Tables Quiz

Passing score: 70%
  1. 1.Which built-in function iterates an array-like table in order from index 1?

  2. 2.player.name and player["name"] refer to the same table entry.

  3. 3.The # operator in front of a table, like #scores, returns the table's ____.