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 true if  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.

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

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

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

getAffixNums
This takes a table  and returns an array containing the numbers of keys with the optional prefix   and the optional suffix. For example, the following code 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, getNumKeys 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  function in its implementation. Although some table keys appear to be ignored, all table keys are accessed when it is run.