utils.table
Table utils.
utils.map
utils.map.multiPairs(t1) | Iterator that returns key/value on multiple tables. |
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. |
utils.map
utils.map.multiPairs(t1)
t1
:
first table to traverse.
iterator function.
utils.table
utils.table.copy(src, dst, overwrite)
src
:
a table to be used as the source of the copy.
dst
:
a table to be used as the destination of the copy.
overwrite
:
boolean value, to enable overwrite of existing field in dst table.
the resulting dst table.
utils.table.diff(t1, t2, norecurse)
t1
:
first table to compare.
t2
:
second table to compare.
norecurse
:
boolean value to disable recursive diff.
the diff result as a table.
> 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)
T
:
table to check.
true or false.
utils.table.keys(T)
T
:
the table to extract keys from.
a list of keys of T table.
utils.table.map(T, func, recursive, inplace)
If inplace is set to true, then the table is modified in place.
The function is called with key and value as a parameter.
T
:
the table to apply map on.
func
:
the function to call for each value of the map.
recursive
:
if non false, the map is executed recusively.
inplace
:
if non false, the table is modified in place.
the table with each value mapped by the function
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)
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.
varargs
:
arguments to pack
a table that contains all the arguments followed by the number of arguments.
utils.table.recursivePairs(t, prefix)
t
:
table to iterate.
prefix
:
path prefix to prepend to the key path returned.
iterator function.
{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)
t
:
table to iterate.
iterator function.