This module includes a number of functions for dealing with Lua tables.
Installation
Documentation
Package items
TableTools.pack(...)(function)- Returns a new table with all parameters stored into keys 1, 2, etc. and with a field
nwith the total number of parameters. Note that the resulting table may not be a sequence. - Parameter:
...(optional) - Returns: (table)
- See also: <http://www.lua.org/manual/5.2/manual.html#pdf-table.pack>
TableTools.selectFirst(n, ...)(function)- Returns the first
narguments in. ... Ifnis negative, arguments are counted from the end of the table. - Parameters:
n(number)...(optional)
- See also:
TableTools.isSequence(t)(function)- Returns
trueif a given table is a sequence. - Parameter:
t(table) - Returns: (boolean)
- See also: <http://stackoverflow.com/a/6080274>
TableTools.size(t)(function)- Returns the number of elements in a table, even if it is not a sequence.
- Parameter:
t(table) - Returns: (number)
- See also: <http://stackoverflow.com/a/2705804>
TableTools.includes(t, elm)(function)- Returns
trueif a given table contains a certain element. - Parameters:
t(table)elm
- Returns: (boolean)
- See also: <http://stackoverflow.com/q/2282444>
TableTools.merge(dest, source)(function)- Merges the content of the second table with the content in the first one.
- Parameters:
- Returns: (table)
- See also: <http://wiki.garrysmod.com/page/table/Merge>
TableTools.isPositiveInteger(v)(function)- This function returns true if the given value is a positive integer, and false if not. Although it doesn't operate on tables, it is included here as it is useful for determining whether a given table key is in the array part or the hash part of a table.
- Parameter:
v - Returns: (boolean)
TableTools.isNan(v)(function)- This function returns true if the given number is a NaN value, and false if not. Although it doesn't operate on tables, it is included here as it is useful for determining whether a value can be a valid table key. Lua will generate an error if a NaN is used as a table key.
- Parameter:
v - Returns: (boolean)
TableTools.shallowClone(t)(function)- This returns a clone of a table. The value returned is a new table, but all subtables and functions are shared. Metamethods are respected, but the returned table will have no metatable of its own.
- Parameter:
t(table) - Returns: (table)
TableTools.removeDuplicates(t)(function)- This removes duplicate values from an array. Non-positive-integer keys are ignored. The earliest value is kept, and all subsequent duplicate values are removed, but otherwise the array order is unchanged.
- Parameter:
t(table) - Returns: (table)
TableTools.numKeys(t)(function)- This takes a table and returns an array containing the numbers of any numerical keys that have non-nil values, sorted in numerical order.
- Parameter:
t(table) - Returns: (table)
TableTools.affixNums(t, prefix, suffix)(function)- This takes a table and returns an array containing the numbers of keys with the specified prefix and suffix. For example, for the table {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", affixNums will return {1, 3, 6}.
- Parameters:
- Returns: (table)
TableTools.numData(t, compress)(function)- Given a table with keys like ("foo1", "bar1", "foo2", "baz2"), returns a table of subtables in the format
- { [1] = {foo = 'text', bar = 'text'}, [2] = {foo = 'text', baz = 'text'} } Keys that don't end with an integer are stored in a subtable named "other". The compress option compresses the table so that it can be iterated over with ipairs.
- Parameters:
- Returns: (table)
TableTools.compressSparseArray(t)(function)- This takes an array with one or more nil values, and removes the nil values while preserving the order, so that the array can be safely traversed with ipairs.
- Parameter:
t(table) - Returns: (table)
TableTools.sparseIpairs(t)(function)- This is an iterator for sparse arrays. It can be used like ipairs, but can handle nil values.
- Parameter:
t(table) - Returns: (function)
TableTools.length(t)(function)- This returns the length of a table, or the first integer key n counting from 1 such that t[n + 1] is nil. It is similar to the operator #, but may return a different value when there are gaps in the array portion of the table. Intended to be used on data loaded with mw.loadData. For other tables, use #. Note: #frame.args in frame object always be set to 0, regardless of the number of unnamed template parameters, so use this function for frame.args.
- Parameter:
t(table) - Returns: (number)
See also
Wikipedia:Module:TableTools for a similar module.