HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to LuaVariablesNumbers & MathStringsTablesConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Table
🔁 Condition & Loop
🧩 Function
Lua›Tables

Tables

Lua's only data structure: arrays (1-indexed) and dictionaries; #t and table.insert.

Simple enough for a 5-year-old to understand

Table (as an array)

In Lua, the first element is t[1] — indexing starts at 1!

Example code
1
local t = {10, 20, 30}
A table with 3 elements.
DataA table with 3 elements.
2
print(t[1])
First element: 10 (from 1!)
Output / InputFirst element: 10 (from 1!)
3
print(#t)
Number of elements: 3
Output / InputNumber of elements: 3
DataOutput / Input
💡 Hint: Unlike other languages, the first element in Lua is t[1], not t[0].

About this topic

The table is Lua's only composite data structure. An array is a table, a dictionary is a table, an object is a table. The language's simplicity largely stems from this choice: there is only one structure you need to learn.

When you use a table like an array, the keys are numbers starting from one. When you use it like a dictionary, the key can be any value — a string, a number, or even another table. Mixing both in the same table is possible, but not recommended.

The most important feature of tables is that they are reference types. When you assign a table to another variable, no copy is created; both names point to the same table. A change you make in one is visible in the other, and this becomes a source of bugs when unexpected.

What you will learn

  • Creating tables
  • Using arrays and dictionaries
  • Adding and removing elements
  • The difference between ipairs and pairs
  • Tables being references

Who is this for?

  • Script writers who will work with data structures
  • Those who will keep in-game inventories or settings lists
  • Those who get references while expecting copies

When to use?

Sequential data, setting sets, and object-like structures are all represented by tables; there are no other options in Lua.

If you don't want its content to change when passing a table to a function, you need to create a copy of it.

Common mistakes

Wrong

local t = {1, 2, 3}
print(t[0])

Tables in array format in Lua start at one; the zeroth element is not defined and returns nil. You don't get an error, you silently get nil and the subsequent operation behaves unexpectedly. This is the most common mistake made by those coming from other languages.

Correct

local t = {1, 2, 3}
print(t[1])

Wrong

local a = {1, 2, 3}
local b = a
b[1] = 99
print(a[1])

Tables are references; b and a point to the same table and 99 is printed on the screen. If you expected a copy, the result is surprising. If you really want an independent copy, you need to transfer the elements one by one into a new table.

Correct

local a = {1, 2, 3}
local b = {}
for i, v in ipairs(a) do b[i] = v end
b[1] = 99
print(a[1])

Wrong

local t = {1, 2, nil, 4}
print(#t)

When there is nil in the middle of an array, the result of the length operator is undefined; it can return 2 or 4 and varies depending on the version. In Lua, nil means "no element", so a sparse array is not considered a valid structure. You shouldn't leave gaps by shifting deleted elements.

Correct

local t = {1, 2, 4}
print(#t)

Frequently asked questions

What is the difference between ipairs and pairs?

ipairs iterates over consecutive numerical keys starting from one and stops at the first gap. pairs, on the other hand, iterates over all keys in the table, but its order is not guaranteed. ipairs is used for arrays, pairs for dictionaries.

How is an element deleted from a table?

By assigning nil to the key in a dictionary. In an array, table.remove is used; this function shifts the remaining elements and leaves no gaps. Directly assigning nil creates a hole in the array and breaks the length calculation.

What is the best way to add an element to a table?

table.insert is used to append to the end; this calculates the length of the array itself. When adding a large number of elements, tracking the index manually is slightly faster, but readability is usually more valuable.

Summary

The table is Lua's only composite data structure.

Array indices start at one.

Tables are references, assignment does not produce a copy.

Nil in the middle of an array breaks the length calculation.

ipairs iterates sequentially, pairs iterates over all keys.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code