Skip to content

Table

Inherits: LibTSMModule

Functions

CopyFrom

lua
function Lua.Table.CopyFrom(tbl, srcTbl)

Copies all entries into one table from another.

Parameters:

NameTypeDescription
tbltable<K, V>The table to copy entries in to
srcTbltable<K, V>The table to copy entries from

Count

lua
function Lua.Table.Count(tbl)
  -> number

Gets the number of entries in the table. This can be used when the count of a non-numerically-indexed table is desired (i.e. #tbl wouldn't work).

Parameters:

NameTypeDescription
tbltableThe table to get the number of entries in

Returns:

TypeDescription
number

Equal

lua
function Lua.Table.Equal(tbl1, tbl2)
  -> boolean

Checks if two tables have the same entries (non-recursively).

Parameters:

NameTypeDescription
tbl1tableThe first table to check
tbl2tableThe second table to check

Returns:

TypeDescription
boolean

Filter

lua
function Lua.Table.Filter(tbl, func, ...)

Uses a function to filter the entries in a table.

Parameters:

NameTypeDescription
tbltableThe table to be filtered
funcfun(key: any, value: any, ...): booleanThe filter function which gets passed key, value, ... and returns true if that entry should be removed from the table
...anyOptional arguments to be passed to the filter function

FilterKeyNotInTable

lua
function Lua.Table.FilterKeyNotInTable(tbl, checkTbl)

Filters entries in a table by checking if keys exist as keys in another table.

Parameters:

NameTypeDescription
tbltable<K, any>The table to be filtered
checkTbltable<K, any>The table to check against

FilterValueNotInTable

lua
function Lua.Table.FilterValueNotInTable(tbl, checkTbl)

Filters entries in a table by checking if values exist as keys in another table.

Parameters:

NameTypeDescription
tbltable<any, V>The table to be filtered
checkTbltable<V, any>The table to check against

GetChangedKeys

lua
function Lua.Table.GetChangedKeys(old, new, result)

Gets the keys which were changed between two tables.

Parameters:

NameTypeDescription
oldtable<string, any>The old table
newtable<string, any>The new table
resulttable<string, true>A result table to store the keys which were changed (with a value of true)

GetCommonValuesSorted

lua
function Lua.Table.GetCommonValuesSorted(tbls, result)

Gets the common values from two or more sorted tables.

Parameters:

NameTypeDescription
tblsV[][]The tables to compare
resultV[]The result table

GetDiffOrdered

lua
function Lua.Table.GetDiffOrdered(old, new, inserted, removed)
  -> boolean

Gets the diff between two lists with order taken into account and returns whether or not we were successful.

Parameters:

NameTypeDescription
oldtableThe old list of items
newtableThe new list of items
insertednumber[]A result table to store the indexes which were added to the new table
removednumber[]A result table to store the indexes which were removed from the old table

Returns:

TypeDescription
boolean

GetDistinctKey

lua
function Lua.Table.GetDistinctKey(tbl, value)
  -> any

Gets the distinct table key by value. This function will assert if the value is not found in the table or if more than one key is found.

Parameters:

NameTypeDescription
tbltableThe table to look through
valueanyThe value to get the key of

Returns:

TypeDescription
any

GetKeys

lua
function Lua.Table.GetKeys(tbl, keys)

Gets the keys from a table and inserts them into another.

Parameters:

NameTypeDescription
tbltable<K, any>The table to get the keys of
keysK[]The table to insert the keys into

InsertFill

lua
function Lua.Table.InsertFill(tbl, startIndex, numValues, fillValue)

Inserts fill values into the table.

Parameters:

NameTypeDescription
tbltableThe table to insert into
startIndexnumberThe index to insert at
numValuesnumberThe number of fill values to insert
fillValueanyThe fill value to insert

InsertFrom

lua
function Lua.Table.InsertFrom(tbl, srcTbl, srcStartIndex, srcEndIndex)

Inserts values at the end of a table from a subrange of another table.

Parameters:

NameTypeDescription
tbltableThe table to insert into
srcTbltableThe table to insert values from
srcStartIndexnumberThe index of the first value in the source table to insert
srcEndIndexnumberThe index of the last value in the source table to insert

InsertFromIterator

lua
function Lua.Table.InsertFromIterator(tbl, iterFunc, iterObj, iterIndex)

Inserts values from an iterator into the table.

Parameters:

NameTypeDescription
tblT[]The table to insert into
iterFuncIteratorObject<fun(): number, T>The iterator function
iterObjanyThe iterator object
iterIndexanyThe iterator index

InsertMultiple

lua
function Lua.Table.InsertMultiple(tbl, ...)

Inserts multiple values into the table.

Parameters:

NameTypeDescription
tbltableThe table to insert into
...anyValues to insert

InsertMultipleAt

lua
function Lua.Table.InsertMultipleAt(tbl, index, ...)

Inserts multiple values into the table at a specified index.

Parameters:

NameTypeDescription
tbltableThe table to insert into
indexnumberThe index to insert at
...anyValues to insert

IsSorted

lua
function Lua.Table.IsSorted(tbl, sortFunc, firstIndex, lastIndex)
  -> boolean

Returns whether or not the table is currently sorted.

Parameters:

NameTypeDescription
tbltableThe table to check
sortFuncfun(a: any, b: any): booleanThe helper function to use to determine sort order
firstIndexnumberThe first index to check (defaults to 1)
lastIndexnumberThe last index to check (defaults to #tbl)

Returns:

TypeDescription
boolean

IsSortedWithValueLookup

lua
function Lua.Table.IsSortedWithValueLookup(tbl, valueLookup, firstIndex, lastIndex)
  -> boolean

Checks whether or not the table is currently sorted with a value lookup table.

Parameters:

NameTypeDescription
tbltabletbl The table to sort
valueLookuptable<any, string | number>valueLookup The sort value lookup table
firstIndexnumberThe first index to check (defaults to 1)
lastIndexnumberThe last index to check (defaults to #tbl)

Returns:

TypeDescription
boolean

KeyByValue

lua
function Lua.Table.KeyByValue(tbl, value)
  -> any

Gets the table key by value.

Parameters:

NameTypeDescription
tbltableThe table to look through
valueanyThe value to get the key of

Returns:

TypeDescription
any

KeyIterator

lua
function Lua.Table.KeyIterator(tbl)
  -> fun():K, table

Creates an iterator from the keys of a table.

Parameters:

NameTypeDescription
tbltable<K, any>The table to iterate over the keys of

Returns:

TypeDescription
fun():KIterator with fields: key
table

MergeSortedWithValueLookup

lua
function Lua.Table.MergeSortedWithValueLookup(tbl1, tbl2, result, valueLookup)

Merges two sorted tables with a value lookup table.

Parameters:

NameTypeDescription
tbl1tableThe first table to merge
tbl2tableThe second table to merge
resulttableThe result table
valueLookuptableThe sort value lookup table

Move

lua
function Lua.Table.Move(tbl, fromIndex, toIndex)

Moves a value in the table from one index to another in an efficient way.

Parameters:

NameTypeDescription
tbltableThe table
fromIndexnumberThe index of the value to move
toIndexnumberThe index to move the value to

RemoveByValue

lua
function Lua.Table.RemoveByValue(tbl, value)
  -> number

Removes all occurrences of the value in the table and returns the number removed. Only the numerically-indexed entries are checked.

Parameters:

NameTypeDescription
tbltableThe table to remove the value from
valueanyThe value to remove

Returns:

TypeDescription
number

RemoveRange

lua
function Lua.Table.RemoveRange(tbl, startIndex, endIndex)

Removes a range of indexes from the table.

Parameters:

NameTypeDescription
tbltableThe table
startIndexnumberThe first index to remove
endIndexnumberThe last index to remove

Reverse

lua
function Lua.Table.Reverse(tbl)

Reverses a table in place.

Parameters:

NameTypeDescription
tbltableThe table to reverse

ReverseIPairs

lua
function Lua.Table.ReverseIPairs(tbl)
  -> fun():number, V, V[], number

Iterates over the table in a similar fasion to ipairs() but in reverse.

Parameters:

NameTypeDescription
tblV[]

Returns:

TypeDescription
fun():number, VIterator with fields: index, value
V[]
number

RotateRight

lua
function Lua.Table.RotateRight(tbl, amount, startIndex, endIndex)
  -> boolean

Rotates the values in a table to the right and returns whether or not the table was changed.

Parameters:

NameTypeDescription
tbltableThe table to rotate
amountnumberThe number of positions to rotate right by (can be negative to rotate left)
startIndexnumberUsed with endIndex to specify a sub-range of the table to rotate
endIndexnumberUsed with startIndex to specify a sub-range of the table to rotate

Returns:

TypeDescription
boolean

SetReadOnly

lua
function Lua.Table.SetReadOnly(tbl)

Sets a table as read-only (modifications aren't checked).

Parameters:

NameTypeDescription
tbltableThe table to make read-only

Sort

lua
function Lua.Table.Sort(tbl, sortFunc)

Sorts a table if it's not already sorted.

Parameters:

NameTypeDescription
tbltableThe table to sort
sortFuncfun(a: any, b: any): booleanThe helper function to use to determine sort order

SortWithValueLookup

lua
function Lua.Table.SortWithValueLookup(tbl, valueLookup, reverse, secondarySortFunc)

Does a table sort with an extra value lookup step.

Parameters:

NameTypeDescription
tbltableThe table to sort
valueLookuptableThe sort value lookup table
reversebooleanReverse the sort order
secondarySortFuncfun(a: any, b: any): booleanA secondary sort function for when the sort values are equal

StrideIterator

lua
function Lua.Table.StrideIterator(tbl, numFields)
  -> fun():number, ...any, table, number

Iterates over a table with a stride. NOTE: This iterator must be run to completion and not be interrupted (i.e. with a break or return).

Parameters:

NameTypeDescription
tbltableThe table to iterate over.
numFieldsnumberThe number of fields to unpack with each iteration

Returns:

TypeDescription
fun():number, ...anyIterator with fields: index, {numFields...}
table
number

UnpackAndWipe

lua
function Lua.Table.UnpackAndWipe(tbl)
  -> ...any

Returns the result of calling unpack() on the table and wipes it.

Parameters:

NameTypeDescription
tbltableThe table

Returns:

TypeDescription
...any

WipeAndDeallocate

lua
function Lua.Table.WipeAndDeallocate(tbl)

Wipes a table and deallocates the extra memory used by its keys.

Parameters:

NameTypeDescription
tbltable