[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ


Tables implement associative arrays. That is, arrays of key/value pairs.

The keys and values can be any type, except nil.

The only way to create a table is by using a "table constructor" like this:

t = {}

The table constructor can also add initial key/value pairs to the table, eg.

foods = {
   fish  = 10,
   chips = 11,
   cake  = 12

You can add elements later or in the constructor. Ultimately either method gives the same end result.

Once you have a table you can get at the values in it by "indexing" into the table. One way is this:

print ( foods ["fish"] )  --> 10

Or in the case of string keys, where the key follows the rules for identifiers, you can use this shortcut:

print ( foods.fish )  --> 10

Tables are anonymous, they don't have to be stored in any variable, or one table might be stored (by reference) in many variables. For example:

print ( { a = 42 } )  --> table: 01D75990

That table existed even though it was not assigned to a variable.

x = { a = 42 }
y = x

In this example both x and y refer to the same table. The assignment did not make a copy of the table, it made a copy of the reference to the table.

Table constructors in the general form provide the key in brackets:

foods = {
   ["fish"]  = 10,
   ["chips"] = 11,
   ["cake"]  = 12

This is equivalent to the earlier example.

For other data types you must use brackets for the key, eg.

squares = {
   [2] = 4,
   [3] = 9,
   [4] = 16,

Both the keys and values can be any type except nil, so a table can contain nested tables, or boolean values, for example.

Lua can also automatically assign keys starting at one, like this:

words = {
  "every",    -- assigned key 1
  "good",     -- assigned key 2
  "boy",      -- assigned key 3
  "deserves", -- assigned key 4
  "fruit",    -- assigned key 5

Elements in tables are separated by a comma or semicolon. It is acceptable to have a trailing comma or semicolon, to assist in machine-generated tables.

Note that there is a difference between the key "10" (a string) and the key 10 (a number).

By convention, in Lua tables start with one (not zero as in C). The Lua libraries are designed around this convention. You can "manually" put an element at position 0 if you need to:

t = {
   [0] = "zero",
   [1] = "one",
   [2] = "two,

You can add elements to a table at any time, eg.

words [6] = "bread"  -- add (or replace) the key/value pair

You can remove elements by assigning nil as their value, eg.

words [2] = nil  -- delete entry for key 2

You can also use table.insert to add numerically keyed items to a table. This will shuffle existing keys upwards to make room if required.

Likewise, you can use table.remove to remove numerically keyed items from a table. This will shuffle existing keys downwards where required.

You do not specifically delete tables. Once there are no references to the table the garbage-collector will delete it. One way of encouraging that to happen is to assign nil to any variables which refer to the table:

words = nil  -- finished with table

You can see if a table is empty or not by using the next function:

print (next (words))  --> prints nil if table is empty

See Also ...

Lua keywords/topics

data types
for (generic)
for (numeric)
if / then / else
logical operators
relational operators
string literals


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 SQLite (database) interface
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting callbacks - plugins

(Help topic: lua=tables)

Documentation contents page

Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]