dreamworkAPI
Ctrl K
GitHub
libraryDocs / table

table

librarysharedmenusource

The table library is a standard Lua library which provides functions to manipulate tables.

Classes & types

Functions

table.clearIndexesEmpties the indexes of the given table.
table.clearKeysEmpties the keys of the given table.
table.copyCreates a shallow copy of the given table.
table.countReturns the count of keys in the given table.
table.createCreates a new table, filled with the given value and size.
table.ejectEjects selected range of values from the given table.
table.extractExtracts selected range of values from the table.
table.flipFlips the keys with values in the given list (table).
table.flippedCreates a flipped version of the given table.
table.foreachExecutes a provided function once for each table value.
table.getReturns the value of the given key path.
table.getIndexReturns the index of the value in the given table.
table.getKeyReturns the key of the value in the given table.
table.injectInjects values from one table to another.
table.insertInserts a value into a table at the specified index.
table.isEmptyReturns true if the given table is empty.
table.isSequentialReturns true if the given table is sequential.
table.keysReturns list (table) of keys and length of this list.
table.mapApplies a function to each value of the table.
table.moveMoves elements from one table to another.
table.pack
table.pairsReturns the list (table) of key/value pairs and length of this list.
table.randomVIReturns a random index and its value from the given sequential table.
table.randomVKReturns a random key and its value from the given table.
table.rangeReturns a range of elements from the given table.
table.removeByRangeRemoves a range of values from the given table.
table.removeByValueRemoves a value from the given table by its value.
table.reverseReverses all indexes in the given table.
table.reversedCreates a reversed by indexes copy of the given table.
table.setSets the value of the given key path.
table.shallowCopyReturns a shallow copy of the given table.
table.shuffleShuffles the given table.
table.sliceReturns a slice of the given table.
table.transformApplies a function to each key/value pair of the table.
table.truncateTruncates the given table.
table.truncatedCreates a truncated copy of the given table.
table.valuesReturns list (table) of values and length of this list.

#table.clearIndexes

sharedmenusource
function table.clearIndexes(tbl: V[], tbl_length?: integer)

Empties the indexes of the given table.

Parameters
NameTypeDescription
tblV[]The table to empty.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.

#table.clearKeys

sharedmenusource
function table.clearKeys(tbl: table<K, V>)

Empties the keys of the given table.

Parameters
NameTypeDescription
tbltable<K, V>The table to empty.

#table.copy

sharedmenunodiscardsource
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
NameTypeDescription
tblTThe table to copy.
Returns
TypeNameDescription
TcopyThe copied table.

#table.count

sharedmenunodiscardsource
function table.count(tbl: table<K, V>): integer

Returns the count of keys in the given table.

Parameters
NameTypeDescription
tbltable<K, V>The table.
Returns
TypeNameDescription
integerkey_countThe count of keys.

#table.create

sharedmenunodiscardsource
function table.create(value: V, ...: integer): V[]

Creates a new table, filled with the given value and size.

Parameters
NameTypeDescription
valueVThe value to fill the table with.
...integerThe sizes of the table.
Returns
TypeNameDescription
V[]tblThe created table.

#table.eject

sharedmenusource
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
NameTypeDescription
tblV[]The table to remove values from.
start_positionoptionalintegerThe start position.
end_positionoptionalintegerThe end position.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]valuesThe removed values.
integervalues_lengthThe count of removed values.

#table.extract

sharedmenunodiscardsource
function table.extract(tbl: V[], start_position?: integer, end_position?: integer, step_size?: integer, tbl_length?: integer): V[], integer

Extracts selected range of values from the table.

Parameters
NameTypeDescription
tblV[]The table to extract values from.
start_positionoptionalintegerThe start position.
end_positionoptionalintegerThe end position.
step_sizeoptionalintegerThe step size.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]valuesThe extracted values.
integerlengthThe length of the extracted values.

#table.flip

sharedmenusource
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
NameTypeDescription
tbltable<K, V>The table to flip.

#table.flipped

sharedmenunodiscardsource
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
NameTypeDescription
tbltable<K, V>The table to flip.
Returns
TypeNameDescription
table<V, K>resultThe flipped table.

#table.foreach

sharedmenusource
function table.foreach(tbl: V[], fn: fun( index: integer, value: V ), tbl_length?: integer)

Executes a provided function once for each table value.

Parameters
NameTypeDescription
tblV[]The table to execute the function on.
fnfun( index: integer, value: V )The function to execute.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up execution.

#table.get

sharedmenunodiscardsource
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
    local t = { a = { b = { c = { d = { e = "e value!" } } } } }
    print( table.get( t, "a.b.c.d.e" ) ) -- e value!
Parameters
NameTypeDescription
tbltableThe table to get the value from.
pathstringThe key path to get.
separatoroptionalintegerThe separator of the key path, default is 0x2E.
Returns
TypeNameDescription
anyvalueThe value of the key path.

#table.getIndex

sharedmenunodiscardsource
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
NameTypeDescription
tblV[]The table to search.
searchableVThe value to search for.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
integer | nilindexThe index of the value.

#table.getKey

sharedmenunodiscardsource
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
NameTypeDescription
tbltable<K, V>The table to search.
searchableVThe value to search for.
Returns
TypeNameDescription
K | nilkeyThe key of the value.

#table.inject

sharedmenusource
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
NameTypeDescription
destinationV[]The destination table.
sourceV[]The source table.
offsetoptionalintegerThe position to inject.
start_positionoptionalintegerThe start position.
end_positionoptionalintegerThe end position.
destination_lengthoptionalintegerThe length of the destination table. Optionally, it should be used to speed up calculations.
source_lengthoptionalintegerThe length of the source table. Optionally, it should be used to speed up calculations.

#table.insert

sharedmenusource
function table.insert(tbl: V[], index: integer, value: V, tbl_length?: integer): integer

Inserts a value into a table at the specified index.

Parameters
NameTypeDescription
tblV[]The table to insert the value into.
indexintegerThe index to insert the value at.
valueVThe value to insert.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
integerindexThe index of the inserted value.
Overloads
function table.insert(tbl: V[], value: V): integer

#table.isEmpty

sharedmenunodiscardsource
function table.isEmpty(tbl: table): boolean

Returns true if the given table is empty.

Parameters
NameTypeDescription
tbltableThe table to check.
Returns
TypeNameDescription
booleanresulttrue if the table is empty, false otherwise.

#table.isSequential

sharedmenunodiscardsource
function table.isSequential(tbl: table): boolean

Returns true if the given table is sequential.

Parameters
NameTypeDescription
tbltableThe table to check.
Returns
TypeNameDescription
booleanis_sequentialtrue if the table is sequential, false otherwise.

#table.keys

sharedmenunodiscardsource
function table.keys(tbl: table<K, V>): K[], integer

Returns list (table) of keys and length of this list.

Parameters
NameTypeDescription
tbltable<K, V>The table.
Returns
TypeNameDescription
K[]key_lstThe list of keys.
integerkey_countThe length of the list.

#table.map

sharedmenunodiscardsource
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
NameTypeDescription
tblV[]The table to map.
fnfun( index: integer, value: V ): V | nilThe function to apply.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up execution.
Returns
TypeNameDescription
V[]mappedThe new mapped sequential table.
integermapped_lengthThe length of the new mapped sequential table.

#table.move

sharedmenusource
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
NameTypeDescription
sourceV[]The source table.
start_positionoptionalintegerThe start position of the source table, defaults to 1.
end_positionoptionalintegerThe end position of the source table, defaults to the length of the source table.
offsetoptionalintegerThe start position of the destination table, defaults to 1.
destinationoptionalV[]The destination table, defaults to the source table.
source_lengthoptionalintegerThe length of the source table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]destinationSelected destination table.

#table.pack

nodiscardsource
function table.pack(...: V): V[]
Parameters
NameTypeDescription
...V
Returns
TypeNameDescription
V[]

#table.pairs

sharedmenunodiscardsource
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
NameTypeDescription
tbltable<K, V>The table.
Returns
TypeNameDescription
table.Pair<K, V>[]resultThe list of key/value pairs ( sequential table ).
integerresult_lengthThe length of the list.

#table.randomVI

sharedmenunodiscardsource
function table.randomVI(tbl: V[], tbl_length?: integer): V, integer | nil

Returns a random index and its value from the given sequential table.

Parameters
NameTypeDescription
tblV[]The sequential table.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
VvalueThe random value.
integer | nilindexThe index of the value.

#table.randomVK

sharedmenunodiscardsource
function table.randomVK(tbl: table<K, V>): V, K

Returns a random key and its value from the given table.

Parameters
NameTypeDescription
tbltable<K, V>The key-value table.
Returns
TypeNameDescription
VvalueThe random value.
KkeyThe key of the value.

#table.range

sharedmenunodiscardsource
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
NameTypeDescription
tblV[]The table to get the range from.
start_positionoptionalintegerThe start position of the range.
end_positionoptionalintegerThe end position of the range.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]range_valuesThe values of the range.
integerrange_lengthThe length of the range.

#table.removeByRange

sharedmenusource
function table.removeByRange(tbl: V[], start_position?: integer, end_position?: integer, tbl_length?: integer)

Removes a range of values from the given table.

Parameters
NameTypeDescription
tblV[]The table to remove from.
start_positionoptionalintegerThe start position.
end_positionoptionalintegerThe end position.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.

#table.removeByValue

sharedmenusource
function table.removeByValue(tbl: V[], value: V, tbl_length?: integer): integer | nil

Removes a value from the given table by its value.

Parameters
NameTypeDescription
tblV[]The table to remove from.
valueVThe value that will be removed.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
integer | nilindexThe index of the value.

#table.reverse

sharedmenusource
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:

OldNew
[ 1 ][ 5 ]
[ 2 ][ 4 ]
[ 3 ][ 3 ]
[ 4 ][ 2 ]
[ 5 ][ 1 ]
Parameters
NameTypeDescription
tblV[]The sequential table.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]tblThe reversed sequential table.
integerlengthThe length of the reversed table.

#table.reversed

sharedmenunodiscardsource
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:

OldNew
[ 1 ][ 5 ]
[ 2 ][ 4 ]
[ 3 ][ 3 ]
[ 4 ][ 2 ]
[ 5 ][ 1 ]
Parameters
NameTypeDescription
tblV[]The table to reverse.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]reversedThe reversed table.
integerlengthThe length of the reversed table.

#table.set

sharedmenusource
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
    local t = {}
    table.set( t, "a.b.c.d.e", "e value!" )
    print( t.a.b.c.d.e ) -- e value!
Parameters
NameTypeDescription
tbltableThe table to set the value in.
pathstringThe key path.
valueanyThe value to set.
separatoroptionalintegerThe separator of the key path, default is 0x2E.

#table.shallowCopy

sharedmenunodiscardsource
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
NameTypeDescription
tblV[]The table to copy.
Returns
TypeNameDescription
V[]The copied table.

#table.shuffle

sharedmenusource
function table.shuffle(tbl: V[], tbl_length?: integer)

Shuffles the given table.

Parameters
NameTypeDescription
tblV[]The table.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.

#table.slice

sharedmenunodiscardsource
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
NameTypeDescription
tblV[]The table to slice.
start_positionoptionalintegerThe start position of the slice.
end_positionoptionalintegerThe end position of the slice.
stepoptionalintegerThe step of the slice.
tbl_lengthoptionalintegerThe length of the table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]sliceThe slice of the table.
integerlengthThe length of the sliced table.

#table.transform

sharedmenunodiscardsource
function table.transform(tbl: table<K, V>, fn: fun( key: K, value: V ): V | nil): table<K, V>

Applies a function to each key/value pair of the table.

The original table remains unchanged.

The returned table is a shallow copy of the original table.

Parameters
NameTypeDescription
tbltable<K, V>The key/value table.
fnfun( key: K, value: V ): V | nilThe function to apply.
Returns
TypeNameDescription
table<K, V>transformedThe new transformed key/value table.

#table.truncate

sharedmenusource
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
NameTypeDescription
tblV[]The table to truncate.
new_lengthoptionalintegerThe length of the truncated table.
tbl_lengthoptionalintegerThe length of the original table. Optionally, it should be used to speed up calculations.

#table.truncated

sharedmenunodiscardsource
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
NameTypeDescription
tblV[]The table to truncate.
tbl_lengthoptionalintegerThe length of the truncated table. Optionally, it should be used to speed up calculations.
Returns
TypeNameDescription
V[]truncatedThe truncated table.

#table.values

sharedmenunodiscardsource
function table.values(tbl: table<K, V>): V[], integer

Returns list (table) of values and length of this list.

Parameters
NameTypeDescription
tbltable<K, V>The table.
Returns
TypeNameDescription
V[]value_lstThe list of values.
integervalue_countThe length of the list.
Generated from the dreamwork meta branch.