Module:TableTools/doc: Difference between revisions

Jump to navigation Jump to search
imported>Mr. Stradivarius
(add size function docs)
imported>Mr. Stradivarius
(put the functions in rough order of usefulness)
Line 19: Line 19:
This function returns <code>true</code> if <code>''num''</code> is a positive integer, and <code>false</code> 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.
This function returns <code>true</code> if <code>''num''</code> is a positive integer, and <code>false</code> 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 ==
== numKeys ==


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


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 <code style="white-space: nowrap;">{foo = "foo", bar = "bar"}</code> and <code style="white-space: nowrap;">{foo = "foo", bar = "baz", qux = "qux"}</code>, union will return <code style="white-space: nowrap;">{foo = "foo", bar = {"bar", "baz"}, qux = "qux"}</code>.
This takes a table <code>''t''</code> 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 <code style="white-space: nowrap;">{1, nil, 2, 3, foo = 'bar'}</code>, numKeys will return <code style="white-space: nowrap;">{1, 3, 4}</code>.


== valueUnion ==
== affixNums ==


<source lang="lua">
<source lang="lua">
TableTools.valueUnion(t1, t2, ...)
local nums = TableTools.affixNums(t, prefix, suffix)
</source>
</source>


This returns the union of the values of n tables, as an array. For example, for the tables <code style="white-space: nowrap;">{1, 3, 4, 5, foo = 7}</code> and <code style="white-space: nowrap;">{2, bar = 3, 5, 6}</code>, valueUnion will return <code style="white-space: nowrap;">{1, 2, 3, 4, 5, 6, 7}</code>.
This takes a table <code>''t''</code> and returns an array containing the numbers of keys with the optional prefix <code>''prefix''</code> and the optional suffix <code>''suffix''</code>. For example, for the table <code style="white-space: nowrap;">{a1 = 'foo', a3 = 'bar', a6 = 'baz'}</code> and the prefix <code>'a'</code>, affixNums will return <code style="white-space: nowrap;">{1, 3, 6}</code>.


== intersection ==
== compressSparseArray ==


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


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 <code style="white-space: nowrap;">{foo = "foo", bar = "bar"}</code> and <code style="white-space: nowrap;">{foo = "foo", bar = "baz", qux = "qux"}</code>, intersection will return <code style="white-space: nowrap;">{foo = "foo"}</code>.
This takes an array <code>''t''</code> 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 <code style="white-space: nowrap;">{1, nil, foo = 'bar', 3, 2}</code>, compressSparseArray will return <code style="white-space: nowrap;">{1, 3, 2}</code>.


== valueIntersection ==
== sparseIpairs ==


<source lang="lua">
<source lang="lua">
TableTools.valueIntersection(t1, t2, ...)
TableTools.sparseIpairs(t)
</source>
</source>


This returns the intersection of the values of n tables, as an array. For example, for the tables <code style="white-space: nowrap;">{1, 3, 4, 5, foo = 7}</code> and <code style="white-space: nowrap;">{2, bar = 3, 5, 6}</code>, valueIntersection will return <code style="white-space: nowrap;">{3, 5}</code>.
This is an iterator function for traversing a sparse array <code>''t''</code>. It is similar to [[mw:Extension:Scribunto/Lua reference manual#ipairs|ipairs]], but will continue to iterate until the highest numerical key, whereas ipairs may stop after the first <code>nil</code> value. Any keys that are not positive integers are ignored.


== numKeys ==
Usually sparseIpairs is used in a generic <code>for</code> loop.


<source lang="lua">
<source lang="lua">
local nums = TableTools.numKeys(t)
for i, v in TableTools.sparseIpairs(t) do
  -- code block
end
</source>
</source>


This takes a table <code>''t''</code> 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 <code style="white-space: nowrap;">{1, nil, 2, 3, foo = 'bar'}</code>, numKeys will return <code style="white-space: nowrap;">{1, 3, 4}</code>.
Note that sparseIpairs uses the [[mw:Extension:Scribunto/Lua reference manual#pairs|pairs]] function in its implementation. Although some table keys appear to be ignored, all table keys are accessed when it is run.


== affixNums ==
== union ==


<source lang="lua">
<source lang="lua">
local nums = TableTools.affixNums(t, prefix, suffix)
TableTools.union(t1, t2, ...)
</source>
</source>


This takes a table <code>''t''</code> and returns an array containing the numbers of keys with the optional prefix <code>''prefix''</code> and the optional suffix <code>''suffix''</code>. For example, for the table <code style="white-space: nowrap;">{a1 = 'foo', a3 = 'bar', a6 = 'baz'}</code> and the prefix <code>'a'</code>, affixNums will return <code style="white-space: nowrap;">{1, 3, 6}</code>.
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 <code style="white-space: nowrap;">{foo = "foo", bar = "bar"}</code> and <code style="white-space: nowrap;">{foo = "foo", bar = "baz", qux = "qux"}</code>, union will return <code style="white-space: nowrap;">{foo = "foo", bar = {"bar", "baz"}, qux = "qux"}</code>.


== compressSparseArray ==
== valueUnion ==


<source lang="lua">
<source lang="lua">
TableTools.compressSparseArray(t)
TableTools.valueUnion(t1, t2, ...)
</source>
</source>


This takes an array <code>''t''</code> 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 <code style="white-space: nowrap;">{1, nil, foo = 'bar', 3, 2}</code>, compressSparseArray will return <code style="white-space: nowrap;">{1, 3, 2}</code>.
This returns the union of the values of n tables, as an array. For example, for the tables <code style="white-space: nowrap;">{1, 3, 4, 5, foo = 7}</code> and <code style="white-space: nowrap;">{2, bar = 3, 5, 6}</code>, valueUnion will return <code style="white-space: nowrap;">{1, 2, 3, 4, 5, 6, 7}</code>.


== sparseIpairs ==
== intersection ==


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


This is an iterator function for traversing a sparse array <code>''t''</code>. It is similar to [[mw:Extension:Scribunto/Lua reference manual#ipairs|ipairs]], but will continue to iterate until the highest numerical key, whereas ipairs may stop after the first <code>nil</code> value. Any keys that are not positive integers are ignored.
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 <code style="white-space: nowrap;">{foo = "foo", bar = "bar"}</code> and <code style="white-space: nowrap;">{foo = "foo", bar = "baz", qux = "qux"}</code>, intersection will return <code style="white-space: nowrap;">{foo = "foo"}</code>.


Usually sparseIpairs is used in a generic <code>for</code> loop.
== valueIntersection ==


<source lang="lua">
<source lang="lua">
for i, v in TableTools.sparseIpairs(t) do
TableTools.valueIntersection(t1, t2, ...)
  -- code block
end
</source>
</source>


Note that sparseIpairs uses the [[mw:Extension:Scribunto/Lua reference manual#pairs|pairs]] function in its implementation. Although some table keys appear to be ignored, all table keys are accessed when it is run.
This returns the intersection of the values of n tables, as an array. For example, for the tables <code style="white-space: nowrap;">{1, 3, 4, 5, foo = 7}</code> and <code style="white-space: nowrap;">{2, bar = 3, 5, 6}</code>, valueIntersection will return <code style="white-space: nowrap;">{3, 5}</code>.


== size ==
== size ==

Revision as of 01:19, 19 December 2013


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.

numKeys

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

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

affixNums

<source lang="lua"> local nums = TableTools.affixNums(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, for the table {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix 'a', affixNums 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}, compressSparseArray 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.

union

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

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 {foo = "foo", bar = "bar"} and {foo = "foo", bar = "baz", qux = "qux"}, union will return {foo = "foo", bar = {"bar", "baz"}, qux = "qux"}.

valueUnion

<source lang="lua"> TableTools.valueUnion(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}, valueUnion will return {1, 2, 3, 4, 5, 6, 7}.

intersection

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

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 {foo = "foo", bar = "bar"} and {foo = "foo", bar = "baz", qux = "qux"}, intersection will return {foo = "foo"}.

valueIntersection

<source lang="lua"> TableTools.valueIntersection(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}, valueIntersection will return {3, 5}.

size

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

This returns the size of a key/value pair table. For example, for the table {foo = 'foo', bar = 'bar'}, size will return 2. The function will also work on arrays, but for arrays it is more efficient to use the # operator. Note that to find the table size, this function uses the pairs function to iterate through all of the table keys.