Extend table
Extensions to the table module.
Credit to rxi's lume lib for some of these functions..
Functions
| copy (t) | Copies a table. |
| deepCopy (t[, lookup_table]) | Fully copies table and nested tables. |
| hasValue (t, val) | Checks if table has a certain value. |
| keyFromValue (t, val) | Get first key containing the specified value in this table. |
| keysFromValue (t, val) | Get all keys that contain the specified value in this table. |
| removeByValue (t, val[, notSeq=false]) | Remove all occurences of this value from this table. |
| removeByFilter (t, filter_func[, notSeq=false]) | Removes values from the table based on a filter function. |
| getKeys (t) | Get a sequential table of all keys in the given table. |
| shuffle (t) | Shuffle the table values around. |
| invert (t) | Switch the key->value pairs around. |
| set (t) | Return a set of this table, removing all duplicate values. |
| forEach (t, func, ...) | Calls a function on every value in the table. |
| count (t[, count_func]) | Count number of entries in the table. |
| random (t) | Get a random value from this table. |
| filter (t, func, retainkeys) | Filter a table using a given function. |
| merge (t1, t2, retainkeys) | Merge two tables. |
| isSequential (t) | Returns whether table is sequential. |
| toString (t, name, nice[, maxdepth[, maxseq]]) | Convert a table to a pretty string. |
Functions
- copy (t)
-
Copies a table.
Only copies references to nested tables.
Parameters:
- t table table to copy
Returns:
-
table
copied table
- deepCopy (t[, lookup_table])
-
Fully copies table and nested tables.
Credit.
Parameters:
- t table table to copy
- lookup_table table used for avoiding infinite loops when recursively copying nested tables, just ignore. (optional)
Returns:
-
table
copied table
- hasValue (t, val)
-
Checks if table has a certain value.
Parameters:
- t table table to check
- val value to find
Returns:
-
bool
- keyFromValue (t, val)
-
Get first key containing the specified value in this table.
Parameters:
- t table table to check
- val value to find
Returns:
-
number or string
key
- keysFromValue (t, val)
-
Get all keys that contain the specified value in this table.
Parameters:
- t table table to check
- val value to find
Returns:
-
table
table of keys
- removeByValue (t, val[, notSeq=false])
-
Remove all occurences of this value from this table.
Assumes the table is sequential unless otherwise specified.
Parameters:
- t table table
- val value to remove
- notSeq bool is table non-sequential (default false)
Returns:
-
table
table t
- removeByFilter (t, filter_func[, notSeq=false])
-
Removes values from the table based on a filter function.
Assumes the table is sequential unless otherwise specified.
Parameters:
- t table table
- filter_func function filter function in format function( key, value ). If this function returns true for a given table key->value pair, that pair is removed from the table.
- notSeq bool is table non-sequential (default false)
Returns:
-
table
table t
Usage:
local numbers = { "one", "two", "three" } table.removeByFilter( numbers, function( k, v ) if (v == "two") return true end end) -- now table numbers = { "one", "three" }
- getKeys (t)
-
Get a sequential table of all keys in the given table.
Parameters:
- t table table
Returns:
-
table
table of keys in t
- shuffle (t)
-
Shuffle the table values around. Essentially randomizing the key->value pairs.
Parameters:
- t table table
Returns:
-
table
table t, now shuffled
- invert (t)
-
Switch the key->value pairs around. Turning them into value->key pairs.
Parameters:
- t table table
Returns:
-
table
new inverted table
- set (t)
-
Return a set of this table, removing all duplicate values.
Parameters:
- t table table
Returns:
-
table
new set
Usage:
local numbers = { "one", "one", "two", "two", "three" } local numberset = table.set( numbers ) -- numberset = { "one", "two", "three" }
- forEach (t, func, ...)
-
Calls a function on every value in the table.
Parameters:
- t table table
- func string or function either a string or function in format function( key, value, ... ), which is called on every value in the table
- ... mixed additional parameters to be passed to the function call
Returns:
-
table
table t
Usage:
local numbers = { "one", "two", "three" } table.forEach( numbers, function( k, v ) numbers[k] = v.."s" end ) -- now table numbers = { "ones", "twos", "threes" } local vectors = { Vector(1,1), Vector(2,1), Vector(3,1) } table.forEach( vectors, "add", Vector(0,1) ) -- calls Vector:add(..) on every table entry -- now table vectors = { Vector(1,2), Vector(2,2), Vector(3,2) }
- count (t[, count_func])
-
Count number of entries in the table.
Can count only specific entries if specified.
Parameters:
- t table table
- count_func function function in format function( key, value ), only counts entries where this function returns true (optional)
Returns:
-
number
count
- random (t)
-
Get a random value from this table.
Parameters:
- t table table
Returns:
-
random value from the table
- filter (t, func, retainkeys)
-
Filter a table using a given function.
Parameters:
- t table table
- func function function in format function( key, value ), table only retains entries where this functions returns true
- retainkeys bool whether to retain the keys of the original table, or create a new sequence
Returns:
-
table
new filtered table
- merge (t1, t2, retainkeys)
-
Merge two tables.
Parameters:
- t1 table first table
- t2 table second table
- retainkeys bool whether to retain the keys of the second table (overriding those of the first table), or just to add behind the sequence of the first table
Returns:
-
table
table t1, with table t2 merged into it
- isSequential (t)
-
Returns whether table is sequential. Meaning only numerical indices are used without gaps.
Parameters:
- t table table
Returns:
-
bool
is table sequential
- toString (t, name, nice[, maxdepth[, maxseq]])
-
Convert a table to a pretty string.
Parameters:
- t table table
- name string name of the table to print
- nice bool whether to add line breaks and indents (bool)
- maxdepth number max depth to print table (optional)
- maxseq number summarizes sequential tables bigger than this number instead of printing them out completely (optional)
Returns:
-
string
readable string presentation of this table