 <?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mwstake.org/w/index.php?action=history&amp;feed=atom&amp;title=Module%3ATableTools%2Fdoc</id>
	<title>Module:TableTools/doc - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mwstake.org/w/index.php?action=history&amp;feed=atom&amp;title=Module%3ATableTools%2Fdoc"/>
	<link rel="alternate" type="text/html" href="https://mwstake.org/w/index.php?title=Module:TableTools/doc&amp;action=history"/>
	<updated>2026-04-29T13:51:00Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.6</generator>
	<entry>
		<id>https://mwstake.org/w/index.php?title=Module:TableTools/doc&amp;diff=1539&amp;oldid=prev</id>
		<title>Mark Hershberger: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://mwstake.org/w/index.php?title=Module:TableTools/doc&amp;diff=1539&amp;oldid=prev"/>
		<updated>2019-04-23T17:56:48Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 13:56, 23 April 2019&lt;/td&gt;
				&lt;/tr&gt;
&lt;!-- diff cache key mediawiki:diff::1.12:old-1538:rev-1539 --&gt;
&lt;/table&gt;</summary>
		<author><name>Mark Hershberger</name></author>
	</entry>
	<entry>
		<id>https://mwstake.org/w/index.php?title=Module:TableTools/doc&amp;diff=1538&amp;oldid=prev</id>
		<title>enwiki&gt;Erutuon: /* length */ true of both</title>
		<link rel="alternate" type="text/html" href="https://mwstake.org/w/index.php?title=Module:TableTools/doc&amp;diff=1538&amp;oldid=prev"/>
		<updated>2019-03-20T11:15:38Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;length: &lt;/span&gt; true of both&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;includeonly&amp;gt;{{Being deleted|2019 February 20|Module:Array_length}}&amp;lt;/includeonly&amp;gt;{{high-use|3,780,000|all-pages = yes}}&lt;br /&gt;
{{module rating|protected}}&lt;br /&gt;
{{used in system}}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
== Loading the module ==&lt;br /&gt;
&lt;br /&gt;
To use any of the functions, first you must load the module.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local TableTools = require('Module:TableTools')&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== isPositiveInteger ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.isPositiveInteger(value)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Returns &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;''value''&amp;lt;/code&amp;gt; is a positive integer, and &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
== isNan ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.isNan(value)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Returns &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; if &amp;lt;code&amp;gt;''value''&amp;lt;/code&amp;gt; is a [[NaN]] value, and &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; 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 value is used as a table key.)&lt;br /&gt;
&lt;br /&gt;
== shallowClone ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.shallowClone(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. If you want to make a new table with no shared subtables and with metatables transferred, you can use &amp;lt;code&amp;gt;[[mw:Extension:Scribunto/Lua reference manual#mw.clone|mw.clone]]&amp;lt;/code&amp;gt; instead. If you want to make a new table with no shared subtables and without metatables transferred, use &amp;lt;code&amp;gt;[[#deepCopy|deepCopy]]&amp;lt;/code&amp;gt; with the &amp;lt;code&amp;gt;noMetatable&amp;lt;/code&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
== removeDuplicates ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.removeDuplicates(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Removes duplicate values from an array. This function is only designed to work with standard arrays: keys that are not positive integers are ignored, as are all values after the first &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; value. (For arrays containing &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; values, you can use &amp;lt;code&amp;gt;[[#compressSparseArray|compressSparseArray]]&amp;lt;/code&amp;gt; first.) The function tries to preserve the order of the array: the earliest non-unique value is kept, and all subsequent duplicate values are removed. For example, for the table {{code|code={5, 4, 4, 3, 4, 2, 2, 1}|lang=lua}} &amp;lt;code&amp;gt;removeDuplicates&amp;lt;/code&amp;gt; will return {{code|code={5, 4, 3, 2, 1}|lang=lua}}&lt;br /&gt;
&lt;br /&gt;
== numKeys ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.numKeys(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Takes a table &amp;lt;code&amp;gt;''t''&amp;lt;/code&amp;gt; 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|code={'foo', nil, 'bar', 'baz', a = 'b'}|lang=lua}}, &amp;lt;code&amp;gt;numKeys&amp;lt;/code&amp;gt; will return {{code|code={1, 3, 4}|lang=lua}}.&lt;br /&gt;
&lt;br /&gt;
== affixNums ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.affixNums(t, prefix, suffix)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Takes a table &amp;lt;code&amp;gt;''t''&amp;lt;/code&amp;gt; and returns an array containing the numbers of keys with the optional prefix &amp;lt;code&amp;gt;''prefix''&amp;lt;/code&amp;gt; and the optional suffix &amp;lt;code&amp;gt;''suffix''&amp;lt;/code&amp;gt;. For example, for the table {{code|code={a1 = 'foo', a3 = 'bar', a6 = 'baz'}|lang=lua}} and the prefix &amp;lt;code&amp;gt;'a'&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;affixNums&amp;lt;/code&amp;gt; will return {{code|code={1, 3, 6}|lang=lua}}. All characters in &amp;lt;code&amp;gt;''prefix''&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;''suffix''&amp;lt;/code&amp;gt; are interpreted literally.&lt;br /&gt;
&lt;br /&gt;
== numData ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.numData(t, compress)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Given a table with keys like &amp;lt;code&amp;gt;&amp;quot;foo1&amp;quot;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;&amp;quot;bar1&amp;quot;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;&amp;quot;foo2&amp;quot;&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;&amp;quot;baz2&amp;quot;&amp;lt;/code&amp;gt;, returns a table of subtables in the format {{code|code={ [1] = {foo = 'text', bar = 'text'}, [2] = {foo = 'text', baz = 'text'} }|lang=lua}}. Keys that don't end with an integer are stored in a subtable named &amp;lt;code&amp;gt;&amp;quot;other&amp;quot;&amp;lt;/code&amp;gt;. The compress option compresses the table so that it can be iterated over with &amp;lt;code&amp;gt;ipairs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== compressSparseArray ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.compressSparseArray(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Takes an array &amp;lt;code&amp;gt;''t''&amp;lt;/code&amp;gt; with one or more nil values, and removes the nil values while preserving the order, so that the array can be safely traversed with &amp;lt;code&amp;gt;ipairs&amp;lt;/code&amp;gt;. Any keys that are not positive integers are removed. For example, for the table {{code|code={1, nil, foo = 'bar', 3, 2}|lang=lua}}, &amp;lt;code&amp;gt;compressSparseArray&amp;lt;/code&amp;gt; will return {{code|code={1, 3, 2}|lang=lua}}.&lt;br /&gt;
&lt;br /&gt;
== sparseIpairs ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.sparseIpairs(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is an iterator function for traversing a sparse array &amp;lt;code&amp;gt;''t''&amp;lt;/code&amp;gt;. It is similar to &amp;lt;code&amp;gt;[[mw:Extension:Scribunto/Lua reference manual#ipairs|ipairs]]&amp;lt;/code&amp;gt;, but will continue to iterate until the highest numerical key, whereas &amp;lt;code&amp;gt;ipairs&amp;lt;/code&amp;gt; may stop after the first &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; value. Any keys that are not positive integers are ignored.&lt;br /&gt;
&lt;br /&gt;
Usually &amp;lt;code&amp;gt;sparseIpairs&amp;lt;/code&amp;gt; is used in a generic &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
for i, v in TableTools.sparseIpairs(t) do&lt;br /&gt;
   -- code block&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that &amp;lt;code&amp;gt;sparseIpairs&amp;lt;/code&amp;gt; uses the &amp;lt;code&amp;gt;[[mw:Extension:Scribunto/Lua reference manual#pairs|pairs]]&amp;lt;/code&amp;gt; function in its implementation. Although some table keys appear to be ignored, all table keys are accessed when it is run.&lt;br /&gt;
&lt;br /&gt;
== size ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.size(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finds the size of a key/value pair table. For example, for the table {{code|code={foo = 'foo', bar = 'bar'}|lang=lua}}, &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; will return &amp;lt;code&amp;gt;2&amp;lt;/code&amp;gt;. The function will also work on arrays, but for arrays it is more efficient to use the &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt; operator. Note that to find the table size, this function uses the &amp;lt;code&amp;gt;[[mw:Extension:Scribunto/Lua reference manual#pairs|pairs]]&amp;lt;/code&amp;gt; function to iterate through all of the table keys.&lt;br /&gt;
&lt;br /&gt;
== keysToList ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.keysToList(t, keySort)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Returns a list of the keys in a table, sorted using either a default comparison function or a custom &amp;lt;code&amp;gt;keySort&amp;lt;/code&amp;gt; function, which  follows the same rules as the &amp;lt;code&amp;gt;comp&amp;lt;/code&amp;gt; function supplied to [[mw:Extension:Scribunto/Lua reference manual#table.sort|&amp;lt;code&amp;gt;table.sort&amp;lt;/code&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
== sortedPairs ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.sortedPairs(t, keySort)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Iterates through a table, with the keys sorted using the &amp;lt;code&amp;gt;keysToList&amp;lt;/code&amp;gt; function. If there are only numerical keys, &amp;lt;code&amp;gt;sparseIpairs&amp;lt;/code&amp;gt; is probably more efficient.&lt;br /&gt;
&lt;br /&gt;
== isArray ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.isArray(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Returns true if all keys in the table are consecutive integers starting at &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== listToSet ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.listToSet(arr)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creates a set from the array part of the table &amp;lt;code&amp;gt;arr&amp;lt;/code&amp;gt;. Indexing the set by any of the values in &amp;lt;code&amp;gt;arr&amp;lt;/code&amp;gt; returns &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local set = TableTools.listToSet { &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot; }&lt;br /&gt;
assert(set[&amp;quot;a&amp;quot;] == true)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== invert ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.invert(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Transposes the keys and values in an array. For example, &amp;lt;code&amp;gt;invert{ &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot; }&amp;lt;/code&amp;gt; yields &amp;lt;code&amp;gt;{ a = 1, b = 2, c = 3 }&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== deepCopy ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.deepCopy(orig, noMetatable, alreadySeen)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creates a copy of the table &amp;lt;code&amp;gt;orig&amp;lt;/code&amp;gt;. As with &amp;lt;code&amp;gt;mw.clone&amp;lt;/code&amp;gt;, all values that are not functions are duplicated and the identity of tables is preserved. If &amp;lt;code&amp;gt;noMetatable&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, then the metatable (if any) is not copied. Can copy tables loaded with &amp;lt;code&amp;gt;mw.loadData&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Similar to &amp;lt;code&amp;gt;mw.clone&amp;lt;/code&amp;gt;, but &amp;lt;code&amp;gt;mw.clone&amp;lt;/code&amp;gt; cannot copy tables loaded with &amp;lt;code&amp;gt;mw.loadData&amp;lt;/code&amp;gt; and does not allow metatables &amp;lt;em&amp;gt;not&amp;lt;/em&amp;gt; to be copied.&lt;br /&gt;
&lt;br /&gt;
== sparseConcat ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.sparseConcat(t, sep)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Concatenates all values in the table that are indexed by a positive integer, in order.&lt;br /&gt;
&lt;br /&gt;
== length ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.length(t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Returns the length of a table, or the first integer key &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; counting from &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; such that &amp;lt;code&amp;gt;t[n + 1]&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. It is similar to the operator &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt;, 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:Extension:Scribunto/Lua reference manual#mw.loadData|&amp;lt;code&amp;gt;mw.loadData&amp;lt;/code&amp;gt;]] and on [[mw:Extension:Scribunto/Lua reference manual#frame.args|&amp;lt;code&amp;gt;frame.args&amp;lt;/code&amp;gt;]]. Both use a metatable such that &amp;lt;code&amp;gt;#mw.loadData(&amp;quot;module:...&amp;quot;)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;#frame.args&amp;lt;/code&amp;gt; don't work correctly. For other tables, use &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== inArray ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
TableTools.inArray(arr, valueToFind)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Returns &amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; inline&amp;gt;true&amp;lt;/syntaxhighlight&amp;gt; if &amp;lt;code&amp;gt;valueToFind&amp;lt;/code&amp;gt; is a member of the array &amp;lt;code&amp;gt;arr&amp;lt;/code&amp;gt;, and &amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot; inline&amp;gt;false&amp;lt;/syntaxhighlight&amp;gt; otherwise.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;includeonly&amp;gt;{{Sandbox other||&lt;br /&gt;
[[Category:Lua metamodules|TableTools]]&lt;br /&gt;
}}&amp;lt;/includeonly&amp;gt;&lt;/div&gt;</summary>
		<author><name>enwiki&gt;Erutuon</name></author>
	</entry>
</feed>