table
The table library is a standard Lua library which provides functions to manipulate tables.
Classes & types Functions
function table.clearIndexes (tbl : V[], tbl_length? : integer )
Empties the indexes of the given table.
Parameters
Name Type Description tblV[]The table to empty. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
function table.clearKeys (tbl : table <K, V>)
Empties the keys of the given table.
Parameters
Name Type Description tbltable <K, V>The table to empty.
function table.copy (tbl : T): T
Creates a shallow copy of the given table.
The original table is not modified.
The returned table is a shallow copy of the original table.
Parameters
Name Type Description tblTThe table to copy.
Returns
Type Name Description TcopyThe copied table.
function table.count (tbl : table <K, V>): integer
Returns the count of keys in the given table.
Parameters
Name Type Description tbltable <K, V>The table.
Returns
Type Name Description integer key_countThe count of keys.
function table.create (value : V, ... : integer ): V[]
Creates a new table, filled with the given value and size.
Parameters
Name Type Description valueVThe value to fill the table with. ...integer The sizes of the table.
Returns
Type Name Description V[]tblThe created table.
function table.eject (tbl : V[], start_position? : integer , end_position? : integer , tbl_length? : integer ): V[], integer
Ejects selected range of values from the given table.
Parameters
Name Type Description tblV[]The table to remove values from. start_positionoptional integer The start position. end_positionoptional integer The end position. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description V[]valuesThe removed values. integer values_lengthThe count of removed values.
function table.flip (tbl : table <K, V>)
Flips the keys with values in the given list (table).
The original table is modified.
The keys are now the values and the values are now the keys.
{ key = value } -> { value = key }
Parameters
Name Type Description tbltable <K, V>The table to flip.
function table.flipped (tbl : table <K, V>): table <V, K>
Creates a flipped version of the given table.
The original table is not modified.
The flipped table has the values as keys and the keys as values.
{ key = value } -> { value = key }
Parameters
Name Type Description tbltable <K, V>The table to flip.
Returns
Type Name Description table <V, K>resultThe flipped table.
function table.foreach (tbl : V[], fn : fun ( index: integer , value: V ), tbl_length? : integer )
Executes a provided function once for each table value.
Parameters
Name Type Description tblV[]The table to execute the function on. fnfun ( index: integer , value: V )The function to execute. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up execution.
function table.get (tbl : table , path : string , separator? : integer ): any
Returns the value of the given key path.
If the key path does not exist, or passes through a non-table value, returns nil.
Every separator splits the path, empty segments are "" keys, so "" is t[ "" ], ".a" is t[ "" ].a, "a." is t.a[ "" ] and "a..b" is t.a[ "" ].b.
Example:
lua Copy
local t = { a = { b = { c = { d = { e = "e value!" } } } } }
print ( table.get ( t, "a.b.c.d.e" ) ) -- e value!
Parameters
Name Type Description tbltable The table to get the value from. pathstring The key path to get. separatoroptional integer The separator of the key path, default is 0x2E.
Returns
Type Name Description any valueThe value of the key path.
function table.getIndex (tbl : V[], searchable : V, tbl_length? : integer ): integer | nil
Returns the index of the value in the given table.
Returns nil if the value is not found in the table.
Parameters
Name Type Description tblV[]The table to search. searchableVThe value to search for. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description integer | nil indexThe index of the value.
function table.getKey (tbl : table <K, V>, searchable : V): K | nil
Returns the key of the value in the given table.
Returns nil if the value is not found in the table.
Parameters
Name Type Description tbltable <K, V>The table to search. searchableVThe value to search for.
Returns
Type Name Description K | nil keyThe key of the value.
function table.inject (destination : V[], source : V[], offset? : integer , start_position? : integer , end_position? : integer , destination_length? : integer , source_length? : integer )
Injects values from one table to another.
Parameters
Name Type Description destinationV[]The destination table. sourceV[]The source table. offsetoptional integer The position to inject. start_positionoptional integer The start position. end_positionoptional integer The end position. destination_lengthoptional integer The length of the destination table. Optionally, it should be used to speed up calculations. source_lengthoptional integer The length of the source table. Optionally, it should be used to speed up calculations.
function table.insert (tbl : V[], index : integer , value : V, tbl_length? : integer ): integer
Inserts a value into a table at the specified index.
Parameters
Name Type Description tblV[]The table to insert the value into. indexinteger The index to insert the value at. valueVThe value to insert. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description integer indexThe index of the inserted value.
Overloads
function table.insert (tbl : V[], value : V): integer
function table.isEmpty (tbl : table ): boolean
Returns true if the given table is empty.
Parameters
Name Type Description tbltable The table to check.
Returns
Type Name Description boolean resulttrue if the table is empty, false otherwise.
function table.isSequential (tbl : table ): boolean
Returns true if the given table is sequential.
Parameters
Name Type Description tbltable The table to check.
Returns
Type Name Description boolean is_sequentialtrue if the table is sequential, false otherwise.
function table.keys (tbl : table <K, V>): K[], integer
Returns list (table) of keys and length of this list.
Parameters
Name Type Description tbltable <K, V>The table.
Returns
Type Name Description K[]key_lstThe list of keys. integer key_countThe length of the list.
function table.map (tbl : V[], fn : fun ( index: integer , value: V ): V | nil , tbl_length? : integer ): V[], integer
Applies a function to each value of the table.
The original table remains unchanged .
The returned table is a shallow copy of the original table.
Parameters
Name Type Description tblV[]The table to map. fnfun ( index: integer , value: V ): V | nil The function to apply. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up execution.
Returns
Type Name Description V[]mappedThe new mapped sequential table. integer mapped_lengthThe length of the new mapped sequential table.
function table.move (source : V[], start_position? : integer , end_position? : integer , offset? : integer , destination? : V[], source_length? : integer ): V[]
Moves elements from one table to another.
Parameters
Name Type Description sourceV[]The source table. start_positionoptional integer The start position of the source table, defaults to 1. end_positionoptional integer The end position of the source table, defaults to the length of the source table. offsetoptional integer The start position of the destination table, defaults to 1. destinationoptional V[]The destination table, defaults to the source table. source_lengthoptional integer The length of the source table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description V[]destinationSelected destination table.
function table.pack (... : V): V[]
Parameters
Returns
function table.pairs (tbl : table <K, V>): table.Pair <K, V>[], integer
Returns the list (table) of key/value pairs and length of this list.
Parameters
Name Type Description tbltable <K, V>The table.
Returns
Type Name Description table.Pair <K, V>[]resultThe list of key/value pairs ( sequential table ). integer result_lengthThe length of the list.
function table.randomVI (tbl : V[], tbl_length? : integer ): V, integer | nil
Returns a random index and its value from the given sequential table.
Parameters
Name Type Description tblV[]The sequential table. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description VvalueThe random value. integer | nil indexThe index of the value.
function table.randomVK (tbl : table <K, V>): V, K
Returns a random key and its value from the given table.
Parameters
Name Type Description tbltable <K, V>The key-value table.
Returns
Type Name Description VvalueThe random value. KkeyThe key of the value.
function table.range (tbl : V[], start_position? : integer , end_position? : integer , tbl_length? : integer ): V[], integer
Returns a range of elements from the given table.
Parameters
Name Type Description tblV[]The table to get the range from. start_positionoptional integer The start position of the range. end_positionoptional integer The end position of the range. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description V[]range_valuesThe values of the range. integer range_lengthThe length of the range.
function table.removeByRange (tbl : V[], start_position? : integer , end_position? : integer , tbl_length? : integer )
Removes a range of values from the given table.
Parameters
Name Type Description tblV[]The table to remove from. start_positionoptional integer The start position. end_positionoptional integer The end position. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
function table.removeByValue (tbl : V[], value : V, tbl_length? : integer ): integer | nil
Removes a value from the given table by its value.
Parameters
Name Type Description tblV[]The table to remove from. valueVThe value that will be removed. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description integer | nil indexThe index of the value.
function table.reverse (tbl : V[], tbl_length? : integer ): V[], integer
Reverses all indexes in the given table.
The original table will be modified .
Example of a reversed table:
Old New [ 1 ][ 5 ][ 2 ][ 4 ][ 3 ][ 3 ][ 4 ][ 2 ][ 5 ][ 1 ]
Parameters
Name Type Description tblV[]The sequential table. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description V[]tblThe reversed sequential table. integer lengthThe length of the reversed table.
function table.reversed (tbl : V[], tbl_length? : integer ): V[], integer
Creates a reversed by indexes copy of the given table.
The original table remains unchanged .
Example of a reversed table:
Old New [ 1 ][ 5 ][ 2 ][ 4 ][ 3 ][ 3 ][ 4 ][ 2 ][ 5 ][ 1 ]
Parameters
Name Type Description tblV[]The table to reverse. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description V[]reversedThe reversed table. integer lengthThe length of the reversed table.
function table.set (tbl : table , path : string , value : any , separator? : integer )
Sets the value of the given key path.
Tables are created if they do not exist, non-table values on the path are replaced.
Every separator splits the path, empty segments are "" keys, so "" is t[ "" ], ".a" is t[ "" ].a, "a." is t.a[ "" ] and "a..b" is t.a[ "" ].b.
Example:
lua Copy
local t = {}
table.set ( t, "a.b.c.d.e" , "e value!" )
print ( t.a.b.c.d.e ) -- e value!
Parameters
Name Type Description tbltable The table to set the value in. pathstring The key path. valueany The value to set. separatoroptional integer The separator of the key path, default is 0x2E.
function table.shallowCopy (tbl : V[]): V[]
Returns a shallow copy of the given table.
NOTE: This function is much faster that any other copy function and method, but works and efficient enough only for small tables (up to 1000 elements).
Parameters
Name Type Description tblV[]The table to copy.
Returns
Type Name Description V[]The copied table.
function table.shuffle (tbl : V[], tbl_length? : integer )
Shuffles the given table.
Parameters
Name Type Description tblV[]The table. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
function table.slice (tbl : V[], start_position? : integer , end_position? : integer , step? : integer , tbl_length? : integer ): V[], integer
Returns a slice of the given table.
Parameters
Name Type Description tblV[]The table to slice. start_positionoptional integer The start position of the slice. end_positionoptional integer The end position of the slice. stepoptional integer The step of the slice. tbl_lengthoptional integer The length of the table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description V[]sliceThe slice of the table. integer lengthThe length of the sliced table.
function table.truncate (tbl : V[], new_length? : integer , tbl_length? : integer )
Truncates the given table.
The original table is modified.
The truncated table is a shallow copy of the original table.
Parameters
Name Type Description tblV[]The table to truncate. new_lengthoptional integer The length of the truncated table. tbl_lengthoptional integer The length of the original table. Optionally, it should be used to speed up calculations.
function table.truncated (tbl : V[], tbl_length? : integer ): V[]
Creates a truncated copy of the given table.
The original table is not modified.
The returned table is a shallow copy of the original table.
Parameters
Name Type Description tblV[]The table to truncate. tbl_lengthoptional integer The length of the truncated table. Optionally, it should be used to speed up calculations.
Returns
Type Name Description V[]truncatedThe truncated table.
function table.values (tbl : table <K, V>): V[], integer
Returns list (table) of values and length of this list.
Parameters
Name Type Description tbltable <K, V>The table.
Returns
Type Name Description V[]value_lstThe list of values. integer value_countThe length of the list.