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
Table (as an array)table.insert / table.removeTable (as a dictionary)
🔁 Condition & Loop
🧩 Function
Lua›REFERENCE›Table
📋

Table

Lua's only data structure: acts as both an array and a dictionary.

CommandDescription
Table (as an array)Ordered values (indexed from 1!).
table.insert / table.removeInsertion and removal.
Table (as a dictionary)Key-value mapping.

Table (as an array)

Try it yourself →

Ordered values (indexed from 1!).

Syntax

t = {e1, e2, e3}

Description

The only structure in Lua is the table. When used as an array, the index starts at 1. #t gives the number of elements.

Example

local t = {10, 20, 30}
print(t[1])
print(#t)

Expected output

10
3

When to use?

Storing lists.

Common mistakes

  • ⚠️ The first element in Lua is t[1], not t[0]!

table.insert / table.remove

Try it yourself →

Insertion and removal.

Syntax

table.insert(t, value)

Description

table.insert adds to the end, table.remove removes from it.

Example

local t = {1, 2}
table.insert(t, 3)
print(#t, t[3])

Expected output

3	3

When to use?

Growing lists.

Table (as a dictionary)

Try it yourself →

Key-value mapping.

Syntax

t = {key = value}

Description

Tables also hold key-values. Accessed via t.key or t["key"].

Example

local person = {name = "Alex", age = 36}
print(person.name)
print(person["age"])

Expected output

Alex
36

When to use?

Structured data.

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