Table

The table object, referred to as arrays in other programming languages, enables the storage of various data types in Lua.

Description

In Lua, table is the primary data structure for storing data. It can be used as arrays, objects, and even classes.

  • Tables are indexed starting from 1.
  • You can create multidimensional (2D) arrays.
  • They are highly flexible and can contain mixed data types.

Constructor

{}

Creates a new table object.

Functions

table comes with a variety of functions to manipulate data:

  • next(): Allows a program to traverse all fields of a table.
  • setmetatable(): Sets the metatable for the table.
  • getmetatable(): Returns the associated metatable.
  • rawset(): Sets the value of table[index] directly, bypassing metatables.
  • rawget(): Returns the value of table[index] directly.
  • pairs(): Used to iterate over all key-value pairs in the table.
  • ipairs(): Iterates numerically over the table.

Instance Methods

Some common instance methods provided by the table library include:

  • table.insert(): Adds an element to the table.
  • table.remove(): Removes an element from the table.
  • table.pack(): Packs elements into a new table.
  • table.unpack(): Unpacks a table into values.
  • table.concat(): Combines the elements of a table into a string.
  • table.sort(): Sorts the elements in a table.
  • table.move(): Moves a range of elements from one part of a table to another.

Examples

This section provides examples of common table operations in Lua.

Create a Table

local myTable = {}
print(type(myTable))

-- Expected Output: table

Get Table Item by Index

local groceries = {
    { id = 3421, name = "Dark Chocolate", price = "$1.99" },
    { id = 4843, name = "Lemon", price = "$0.69" },
    { id = 2903, name = "Blender", price = "$42.00" }
}

print(groceries[3].name)

-- Expected Output: Blender

Display Values of a Table

local groceries = {
    { id = 3421, name = "Dark Chocolate", price = "$1.99" },
    { id = 4843, name = "Lemon", price = "$0.69" },
    { id = 2903, name = "Blender", price = "$42.00" }
}

for index, data in ipairs(groceries) do
    print(index)
    for key, value in pairs(data) do
        print('\t', key, value)
    end
end

-- Expected Output:
-- 1
-- \t id 3421
-- \t name Dark Chocolate
-- \t price $1.99
-- 2
-- \t id 4843
-- \t name Lemon
-- \t price $0.69
-- 3
-- \t id 2903
-- \t name Blender
-- \t price $42.00

Resemblance of a Class

local Class = {}

function Class.makeSound(sound)
    return sound
end

function Class.sit()
    -- code...
end

print(Class.makeSound("Woof!"))

-- Expected Output: Woof!

Example for Other Programming Languages

-- Simulating an array-like structure (similar to JavaScript or Python)
local groceries = { "Bread", "Milk", "Eggs" }

print(table.concat(groceries, ", "))

-- Expected Output: Bread, Milk, Eggs

Notes

The table library provides generic functions for table manipulation. It offers flexibility for building complex data structures while maintaining performance. When using operations dependent on table length, ensure proper indexing to avoid unexpected behavior.

For more advanced examples, explore additional Lua documentation or community resources.

Was this page helpful?

Table of Contents