Register forum user name Search FAQ

Lua table functions

Lua table functions

These are the functions in the "table" table.

Tables are collections of key/value pairs, where both the key and the value can be any Lua data type, except nil.

Tables are indexed by the key, so if you know the key you can quickly retrieve the value. If you don't know the keys you can search the table using the "pairs" or "ipairs" functions.

Since keys and values can be any data type (except nil) it follows that tables can contain other tables.

A table's size (which is only relevant for numerically-indexed tables) is one less than the first integer index with a nil value. If the table has "holes" in it - that is, numeric keys with gaps in the sequence - then the table size is not guaranteed to be the the last gap. Lua does a binary search to try to find a gap, it does not necessarily find the first or last one.

If you are planning to treat your tables as vectors (that is, with numeric keys and a size) then we recommend that you do not allow gaps in the sequence.

If you want to know if a table is empty, or not, you can test:


if next (t) == nil then
  -- table t is empty
end -- if empty


The behaviour of tables can be modified somewhat by attaching a "metatable" to them with the setmetatable function.

Lua functions

table.concat - Concatenates table items together into a string
table.foreach - Applies a function to each item in a table
table.foreachi - Applies a function to each item in a numerically-keyed table
table.getn - Returns the size of a numerically-keyed table
table.insert - Inserts a new item into a numerically-keyed table
table.maxn - Returns the highest numeric key in the table
table.remove - Removes an item from a numerically-keyed table
table.setn - Sets the size of a table (obsolete)
table.sort - Sorts a table

Topics

Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua string functions
Lua syntax
Lua utilities
Scripting
Scripting callbacks - plugins

(Help topic: general=lua_tables)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.