Skip to content
LordMidas edited this page Dec 27, 2022 · 6 revisions

File path: scripts/config/msu/table.nut

Functions related to interacting with and manipulating a table.

rand

::MSU.Table.rand( _table );
// _table is a table

Returns a random key/value pair from _table as a len 2 array with index 0 being the key and index 1 being the value.

randKey

::MSU.Table.randKey( _table );
// _table is a table

Returns a random key from _table. Does not return its value.

randValue

::MSU.Table.randValue( _table );
// _table is a table

Returns a random value from _table. Does not return its key.

merge

::MSU.Table.merge( _table1, _table2, _overwrite = true, _recursively = false );
// _table1 and _table2 are tables
// _overwrite is a boolean
// _recursively is a boolean

Adds all the keys and values from _table2 to _table1 and returns _table1. If _overwrite is true then if the same key is found in both tables, it replaces its value in _table1 using the value from _table2. If _overwrite is false then it throws an MSU DuplicateKey exception if the same key is found in both tables. If _recursively is set to true, merge will also be called on any tables within table2._

keys

::MSU.Table.keys( _table );
// _table is a table

Returns an array containing all the keys in _table.

values

::MSU.Table.values( _table );
// _table is a table

Returns an array containing all the values in _table.

apply

::MSU.Table.apply( _table, _function );
// _table is a table
// _function is a function with two arguments: _key, _value
// _function must return a new value to replace `_value` for `_key`

Iterates over all the keys and values in _table, passing them as parameters to _function. Stores the return value of _function as the new value for _key in _table. Returns nothing as it modifies _table in place.

filter

::MSU.Table.filter( _table, _function );
// _table is a table
// _function is a function with two arguments: _key, _value
// _function must return a boolean

Creates a new table and iterates over all the keys and values in _table, passing them as parameters to _function. If _function returns true, then stores this key/value pair in the new table. Returns the new table.

map

::MSU.Table.filter( _table, _function );
// _table is a table
// _function is a function with two arguments: _key, _value
// _function must return a length 2 array with index 0 being the new key and index 1 being the new value

Creates a new table and iterates over all the keys and values in _table, passing them as parameters to _function. Uses the return value of _function to create a new key/value pair in the new table. Returns the new table.

Clone this wiki locally