Module:TableTools/doc

This module includes a number of functions for dealing with Lua tables. It is a meta-module, meant to be called from other Lua modules, and should not be called directly from #invoke.

Loading the module
To use any of the functions, first you must load the module.

isPositiveInteger
This function returns  if   is a positive integer, and   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.

union
This returns the union of the key/value pairs of n tables. If any of the tables contain different values for the same table key, the table value is converted to an array holding all of the different values. For example, for the tables  and , union will return.

valueUnion
This returns the union of the values of n tables, as an array. For example, for the tables  and , valueUnion will return.

intersection
This returns the intersection of the key/value pairs of n tables. Both the key and the value must match to be included in the resulting table. For example, for the tables  and , intersection will return.

valueIntersection
This returns the intersection of the values of n tables, as an array. For example, for the tables  and , valueIntersection will return.

numKeys
This takes a table  and returns an array containing the numbers of any positive integer keys that have non-nil values, sorted in numerical order. For example, for the table, numKeys will return.

affixNums
This takes a table  and returns an array containing the numbers of keys with the optional prefix   and the optional suffix. For example, for the table  and the prefix , affixNums will return.

compressSparseArray
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. Any keys that are not positive integers are removed. For example, for the table, compressSparseArray will return.

sparseIpairs
This is an iterator function for traversing a sparse array. It is similar to ipairs, but will continue to iterate until the highest numerical key, whereas ipairs may stop after the first  value. Any keys that are not positive integers are ignored.

Usually sparseIpairs is used in a generic  loop.

Note that sparseIpairs uses the pairs function in its implementation. Although some table keys appear to be ignored, all table keys are accessed when it is run.