Module:TableTools/doc

Revision as of 14:51, 15 December 2013 by imported>Mr. Stradivarius (→‎getAffixNums: make the example inline, as that is probably easier to understand)
Jump to navigation Jump to search

This is the documentation page for Module:TableTools


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.

<source lang="lua"> local TableTools = require('Module:TableTools') </source>

isPositiveInteger

<source lang="lua"> TableTools.isPositiveInteger(num) </source>

This function returns true if num 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

<source lang="lua"> local union = TableTools.getUnion(t1, t2, ...) </source>

This returns the union of the values of n tables, as an array. For example, for the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, getUnion will return {1, 2, 3, 4, 5, 6, 7}.

getIntersection

<source lang="lua"> local intersection = TableTools.getIntersection(t1, t2, ...) </source>

This returns the intersection of the values of n tables, as an array. For example, for the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, getIntersection will return {3, 5}.

getNumKeys

<source lang="lua"> local nums = TableTools.getNumKeys(t) </source>

This takes a table t 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 {1, nil, 2, 3, foo = 'bar'}, getNumKeys will return {1, 3, 4}.

getAffixNums

<source lang="lua"> local nums = TableTools.getAffixNums(t, prefix, suffix) </source>

This takes a table t and returns an array containing the numbers of keys with the optional prefix prefix and the optional suffix suffix. For example, the code TableTools.getAffixNums({a1 = 'foo', a3 = 'bar', a6 = 'baz'}, 'a') will return {1, 3, 6}.

compressSparseArray

<source lang="lua"> TableTools.compressSparseArray(t) </source>

This takes an array t 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 {1, nil, foo = 'bar', 3, 2}, getNumKeys will return {1, 3, 2}.

sparseIpairs

<source lang="lua"> TableTools.sparseIpairs(t) </source>

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

Usually sparseIpairs is used in a generic for loop.

<source lang="lua"> for i, v in TableTools.sparseIpairs(t) do

  -- code block

end </source>

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.