table

A built-in type representing associative arrays, see details


Functions

clear(t : table)

Recursively clears and removes all table elements.

copy(t : table)

->table

Copy the metatable and all first level elements of the given table into a new table. Use table.rcopy to do a recursive copy of all elements

count(t : table)

Count the number of items of a table, also works for non index based tables (using pairs).

examples:

t = {["a"]=1, ["b"]=1}; print(table.count(t)) --> 2

create(t : table?)

->table | tablelib

Create a new, or convert an exiting table to an object that uses the global 'table.XXX' functions as methods, just like strings in Lua do.

examples:

t = table.create(); t:insert("a"); rprint(t) -> [1] = a;
t = table.create{1,2,3}; print(t:concat("|")); -> "1|2|3";

find(t : table, value : any, start_index : integer?)

->key_or_nil : string | number?

Find first match of value in the given table, starting from element number start_index.
Returns the first key that matches the value or nil

examples:

t = {"a", "b"}; table.find(t, "a") --> 1
t = {a=1, b=2}; table.find(t, 2) --> "b"
t = {"a", "b", "a"}; table.find(t, "a", 2) --> "3"
t = {"a", "b"}; table.find(t, "c") --> nil

is_empty(t : table)

->boolean

Returns true when the table is empty, else false and will also work for non indexed tables

examples:

t = {};          print(table.is_empty(t)); -> true;
t = {66};        print(table.is_empty(t)); -> false;
t = {["a"] = 1}; print(table.is_empty(t)); -> false;

keys(t : table)

->table

Return an indexed table of all keys that are used in the table.

examples:

t = {a="aa", b="bb"}; rprint(table.keys(t)); --> "a", "b"
t = {"a", "b"};       rprint(table.keys(t)); --> 1, 2

rcopy(t : table)

->table

Deeply copy the metatable and all elements of the given table recursively into a new table - create a clone with unique references.

values(t : table)

->table

Return an indexed table of all values that are used in the table

examples:

 t = {a="aa", b="bb"}; rprint(table.values(t)); --> "aa", "bb"
 t = {"a", "b"};       rprint(table.values(t)); --> "a", "b"