Module lib.tabutil
table utility
The norns script reference has examples for this module.
Functions
print (t) | print the contents of a table |
sort (t) | return a lexigraphically sorted array of keys for a table |
count (t) | count the number of entries in a table; unlike table.getn() or #table, nil entries won't break the loop |
contains (t, e) | search table for element |
invert (t) | given a simple table of primitives, "invert" it so that values become keys and vice versa. |
key (t, e) | search table for element, return key |
lines (str) | split multi-line string into table of strings |
split (inputstr, sep) | split string into table with delimiter |
save (tbl, filename) | Save a table to disk. |
load (sfile) | Load a table that has been saved via the tab.save() function. |
readonly (params) | Create a read-only proxy for a given table. |
gather (default_values, custom_values) | return new table, gathering values: - first from default_values, - then from (i.e. |
update (table_to_mutate, updated_values) | mutate first table, updating values from second table. |
select_values (tbl, condition) | Create a new table with all values that pass the test implemented by the provided function. |
Functions
- print (t)
-
print the contents of a table
Parameters:
- t table table to print
- sort (t)
-
return a lexigraphically sorted array of keys for a table
Parameters:
- t table table to sort
Returns:
-
table
sorted table
- count (t)
-
count the number of entries in a table;
unlike table.getn() or #table, nil entries won't break the loop
Parameters:
- t table table to count
Returns:
-
number
count
- contains (t, e)
-
search table for element
Parameters:
- t table table to check
- e element to look for
Returns:
-
boolean
t/f is element is present
- invert (t)
-
given a simple table of primitives,
"invert" it so that values become keys and vice versa.
this allows more efficient checks on multiple values
Parameters:
- t a simple table
- key (t, e)
-
search table for element, return key
Parameters:
- t table table to check
- e element to look for
Returns:
-
key, nil if not found
- lines (str)
-
split multi-line string into table of strings
Parameters:
- str string string with line breaks
Returns:
-
table
table with entries for each line
- split (inputstr, sep)
-
split string into table with delimiter
Parameters:
- save (tbl, filename)
-
Save a table to disk.
Saves tables, numbers, booleans and strings.
Inside table references are saved.
Does not save userdata, metatables, functions and indices of these.
Based on http://lua-users.org/wiki/SaveTableToFile by ChillCode.
Parameters:
Returns:
-
On failure, returns an error msg.
- load (sfile)
-
Load a table that has been saved via the tab.save() function.
Parameters:
- sfile string Filename or stringtable to load.
Returns:
-
On success, returns a previously saved table. On failure, returns as second argument an error msg.
- readonly (params)
-
Create a read-only proxy for a given table.
Parameters:
- params params.table is the table to proxy, params.except a list of writable keys, params.expose limits which keys from params.table are exposed (optional)
Returns:
-
table
the proxied read-only table
- gather (default_values, custom_values)
-
return new table, gathering values:
- first from defaultvalues,
- then from (i.e. overridden by) customvalues
nils in custom_values are ignored
Parameters:
- default_values table base values (provides keys & fallback values)
- custom_values table override values (take precedence)
Returns:
-
table
composite table
- update (table_to_mutate, updated_values)
-
mutate first table, updating values from second table.
new keys from second table will be added to first.
nils in updated_values are ignored
Parameters:
Returns:
-
table
composite table
- select_values (tbl, condition)
-
Create a new table with all values that pass the test implemented by the provided function.
Parameters:
- tbl table table to check
- condition callback function that tests all values of provided table, passes value and key as arguments
Returns:
-
table
table with values that pass the test