Module utils.table Table utils.

Type utils.map

utils.map.multiPairs(t1) Iterator that returns key/value on multiple tables.

Type utils.table

utils.table.copy(src, dst, overwrite) Copies a table from the source to destination table.
utils.table.diff(t1, t2, norecurse) Produces a diff of two tables.
utils.table.isArray(T) Checks if the table is regular array.
utils.table.keys(T) Extracts the keys of a table into a list.
utils.table.map(T, func, recursive, inplace) Executes a function on each element of a table.
utils.table.pack(varargs) Packs a variable number of arguments into a table.
utils.table.recursivePairs(t, prefix) Iterator that returns key/value pairs except walking through sub tables and concatenating the path in the key.
utils.table.sortedPairs(t) Iterator that iterates on the table in key ascending order.

Type utils.map

Field(s)

utils.map.multiPairs(t1)

Iterator that returns key/value on multiple tables.

Parameter

Return value

iterator function.

Type utils.table

Field(s)

utils.table.copy(src, dst, overwrite)

Copies a table from the source to destination table.
The copy is recursive.

Parameters

Return value

the resulting dst table.

utils.table.diff(t1, t2, norecurse)

Produces a diff of two tables.
Recursive diff by default.

Parameters

Return value

the diff result as a table.

Usage:

> t1 = { a = 1, b=3, c = "foo"}
> t2 = { a = 1, b=3, c = "foo"}
> :diff(t1, t2)
= {  }
> t1 = { a = 1, b=3, c = "foo"}
> t2 = { a = 1, b=4, c = "foo"}
> :diff(t1, t2)
= { "b" }
> t1 = { a = 1, b=3, c = "foo"}
> t2 = { a = 1, b=3}
> :diff(t1, t2)
= { "c" }
> t1 = { b=3, c = "foo", d="bar"}
> t2 = { a = 1, b=3, c = "foo"}
> :diff(t1, t2)
= {
   "a",
   "d" }

utils.table.isArray(T)

Checks if the table is regular array.
Regular array is a table with only conscutive integer keys (no holes).

Parameter

Return value

true or false.

utils.table.keys(T)

Extracts the keys of a table into a list.

Parameter

Return value

a list of keys of T table.

utils.table.map(T, func, recursive, inplace)

Executes a function on each element of a table.
If recursive is non false the map function will be called recursively.

If inplace is set to true, then the table is modified in place.

The function is called with key and value as a parameter.

Parameters

Return value

the table with each value mapped by the function

Usage:

local data = { key1 = "val1", key2 = "val2"}
 local function foo(key, value) print("foo:" key, value) end
 map(data, foo)
 -> will print:
 foo:    key1        val1
 foo:    key2        val2

utils.table.pack(varargs)

Packs a variable number of arguments into a table.
This is a temporary function until we switch to Lua 5.2.

Returns a new table with all parameters stored into keys 1, 2, etc. and with a field "n" with the total number of parameters. Also returns, as a second result, the total number of parameters.

Note that the resulting table may not be a sequence.

Note that when loading utils.table module, this pack function is also added to global/std table module.

Parameter

Return value

a table that contains all the arguments followed by the number of arguments.

utils.table.recursivePairs(t, prefix)

Iterator that returns key/value pairs except walking through sub tables and concatenating the path in the key.
This iterator break cycles: it does not recur. If it detects a cycle, the entry causing a cycle is ignored. However, if a table is repeated, its contents will be repeated by this function.

Parameters

Return value

iterator function.

Usage:

{toto={titi=1, tutu=2}, tata = 3, tonton={4, 5}} will iterate through
 ("toto.titi",1), ("toto.tutu",2), ("tata",3) ("tonton.1", 4), ("tonton.2"=5)

utils.table.sortedPairs(t)

Iterator that iterates on the table in key ascending order.

Parameter

Return value

iterator function.