https://de.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3ATableTools Modul:TableTools - Versionsgeschichte 2025-06-12T17:46:09Z Versionsgeschichte dieser Seite in Wikipedia MediaWiki 1.45.0-wmf.4 https://de.wikipedia.org/w/index.php?title=Modul:TableTools&diff=188410463&oldid=prev Capankajsmilyo am 10. Mai 2019 um 03:58 Uhr 2019-05-10T03:58:14Z <p></p> <a href="//de.wikipedia.org/w/index.php?title=Modul:TableTools&amp;diff=188410463&amp;oldid=149518328">Änderungen zeigen</a> Capankajsmilyo https://de.wikipedia.org/w/index.php?title=Modul:TableTools&diff=149518328&oldid=prev I18n: unchanged from c:Module:TableTools ; wird von anderen Modulen aufgerufen z.B. Modul:Erste Schritte 2015-12-28T05:14:54Z <p>unchanged from <a href="https://commons.wikimedia.org/wiki/Module:TableTools" class="extiw" title="c:Module:TableTools">c:Module:TableTools</a> ; wird von anderen Modulen aufgerufen z.B. <a href="/wiki/Modul:Erste_Schritte" title="Modul:Erste Schritte">Modul:Erste Schritte</a></p> <p><b>Neue Seite</b></p><div>--[[<br /> ------------------------------------------------------------------------------------<br /> -- TableTools --<br /> -- --<br /> -- This module includes a number of functions for dealing with Lua tables. --<br /> -- It is a meta-module, meant to be called from other Lua modules, and should --<br /> -- not be called directly from #invoke. --<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> <br /> local libraryUtil = require(&#039;libraryUtil&#039;)<br /> <br /> local p = {}<br /> <br /> -- Define often-used variables and functions.<br /> local floor = math.floor<br /> local infinity = math.huge<br /> local checkType = libraryUtil.checkType<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- isPositiveInteger<br /> --<br /> -- This function returns true if the given value is a positive integer, and false<br /> -- if not. Although it doesn&#039;t operate on tables, it is included here as it is<br /> -- useful for determining whether a given table key is in the array part or the<br /> -- hash part of a table.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.isPositiveInteger(v)<br /> if type(v) == &#039;number&#039; and v &gt;= 1 and floor(v) == v and v &lt; infinity then<br /> return true<br /> else<br /> return false<br /> end<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- isNan<br /> --<br /> -- This function returns true if the given number is a NaN value, and false<br /> -- if not. Although it doesn&#039;t operate on tables, it is included here as it is<br /> -- useful for determining whether a value can be a valid table key. Lua will<br /> -- generate an error if a NaN is used as a table key.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.isNan(v)<br /> if type(v) == &#039;number&#039; and tostring(v) == &#039;-nan&#039; then<br /> return true<br /> else<br /> return false<br /> end<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- shallowClone<br /> --<br /> -- This returns a clone of a table. The value returned is a new table, but all<br /> -- subtables and functions are shared. Metamethods are respected, but the returned<br /> -- table will have no metatable of its own.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.shallowClone(t)<br /> local ret = {}<br /> for k, v in pairs(t) do<br /> ret[k] = v<br /> end<br /> return ret<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- removeDuplicates<br /> --<br /> -- This removes duplicate values from an array. Non-positive-integer keys are<br /> -- ignored. The earliest value is kept, and all subsequent duplicate values are<br /> -- removed, but otherwise the array order is unchanged.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.removeDuplicates(t)<br /> checkType(&#039;removeDuplicates&#039;, 1, t, &#039;table&#039;)<br /> local isNan = p.isNan<br /> local ret, exists = {}, {}<br /> for i, v in ipairs(t) do<br /> if isNan(v) then<br /> -- NaNs can&#039;t be table keys, and they are also unique, so we don&#039;t need to check existence.<br /> ret[#ret + 1] = v<br /> else<br /> if not exists[v] then<br /> ret[#ret + 1] = v<br /> exists[v] = true<br /> end<br /> end <br /> end<br /> return ret<br /> end <br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- numKeys<br /> --<br /> -- This takes a table and returns an array containing the numbers of any numerical<br /> -- keys that have non-nil values, sorted in numerical order.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.numKeys(t)<br /> checkType(&#039;numKeys&#039;, 1, t, &#039;table&#039;)<br /> local isPositiveInteger = p.isPositiveInteger<br /> local nums = {}<br /> for k, v in pairs(t) do<br /> if isPositiveInteger(k) then<br /> nums[#nums + 1] = k<br /> end<br /> end<br /> table.sort(nums)<br /> return nums<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- affixNums<br /> --<br /> -- This takes a table and returns an array containing the numbers of keys with the<br /> -- specified prefix and suffix. For example, for the table<br /> -- {a1 = &#039;foo&#039;, a3 = &#039;bar&#039;, a6 = &#039;baz&#039;} and the prefix &quot;a&quot;, affixNums will<br /> -- return {1, 3, 6}.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.affixNums(t, prefix, suffix)<br /> checkType(&#039;affixNums&#039;, 1, t, &#039;table&#039;)<br /> checkType(&#039;affixNums&#039;, 2, prefix, &#039;string&#039;, true)<br /> checkType(&#039;affixNums&#039;, 3, suffix, &#039;string&#039;, true)<br /> <br /> local function cleanPattern(s)<br /> -- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally.<br /> s = s:gsub(&#039;([%(%)%%%.%[%]%*%+%-%?%^%$])&#039;, &#039;%%%1&#039;)<br /> return s<br /> end<br /> <br /> prefix = prefix or &#039;&#039;<br /> suffix = suffix or &#039;&#039;<br /> prefix = cleanPattern(prefix)<br /> suffix = cleanPattern(suffix)<br /> local pattern = &#039;^&#039; .. prefix .. &#039;([1-9]%d*)&#039; .. suffix .. &#039;$&#039;<br /> <br /> local nums = {}<br /> for k, v in pairs(t) do<br /> if type(k) == &#039;string&#039; then <br /> local num = mw.ustring.match(k, pattern)<br /> if num then<br /> nums[#nums + 1] = tonumber(num)<br /> end<br /> end<br /> end<br /> table.sort(nums)<br /> return nums<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- numData<br /> --<br /> -- Given a table with keys like (&quot;foo1&quot;, &quot;bar1&quot;, &quot;foo2&quot;, &quot;baz2&quot;), returns a table<br /> -- of subtables in the format <br /> -- { [1] = {foo = &#039;text&#039;, bar = &#039;text&#039;}, [2] = {foo = &#039;text&#039;, baz = &#039;text&#039;} }<br /> -- Keys that don&#039;t end with an integer are stored in a subtable named &quot;other&quot;.<br /> -- The compress option compresses the table so that it can be iterated over with<br /> -- ipairs.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.numData(t, compress)<br /> checkType(&#039;numData&#039;, 1, t, &#039;table&#039;)<br /> checkType(&#039;numData&#039;, 2, compress, &#039;boolean&#039;, true)<br /> local ret = {}<br /> for k, v in pairs(t) do<br /> local prefix, num = mw.ustring.match(tostring(k), &#039;^([^0-9]*)([1-9][0-9]*)$&#039;)<br /> if num then<br /> num = tonumber(num)<br /> local subtable = ret[num] or {}<br /> if prefix == &#039;&#039; then<br /> -- Positional parameters match the blank string; put them at the start of the subtable instead.<br /> prefix = 1<br /> end<br /> subtable[prefix] = v<br /> ret[num] = subtable<br /> else<br /> local subtable = ret.other or {}<br /> subtable[k] = v<br /> ret.other = subtable<br /> end<br /> end<br /> if compress then<br /> local other = ret.other<br /> ret = p.compressSparseArray(ret)<br /> ret.other = other<br /> end<br /> return ret<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- compressSparseArray<br /> --<br /> -- This takes an array with one or more nil values, and removes the nil values<br /> -- while preserving the order, so that the array can be safely traversed with<br /> -- ipairs.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.compressSparseArray(t)<br /> checkType(&#039;compressSparseArray&#039;, 1, t, &#039;table&#039;)<br /> local ret = {}<br /> local nums = p.numKeys(t)<br /> for _, num in ipairs(nums) do<br /> ret[#ret + 1] = t[num]<br /> end<br /> return ret<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- sparseIpairs<br /> --<br /> -- This is an iterator for sparse arrays. It can be used like ipairs, but can<br /> -- handle nil values.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.sparseIpairs(t)<br /> checkType(&#039;sparseIpairs&#039;, 1, t, &#039;table&#039;)<br /> local nums = p.numKeys(t)<br /> local i = 0<br /> local lim = #nums<br /> return function ()<br /> i = i + 1<br /> if i &lt;= lim then<br /> local key = nums[i]<br /> return key, t[key]<br /> else<br /> return nil, nil<br /> end<br /> end<br /> end<br /> <br /> --[[<br /> ------------------------------------------------------------------------------------<br /> -- size<br /> --<br /> -- This returns the size of a key/value pair table. It will also work on arrays,<br /> -- but for arrays it is more efficient to use the # operator.<br /> ------------------------------------------------------------------------------------<br /> --]]<br /> function p.size(t)<br /> checkType(&#039;size&#039;, 1, t, &#039;table&#039;)<br /> local i = 0<br /> for k in pairs(t) do<br /> i = i + 1<br /> end<br /> return i<br /> end<br /> <br /> return p</div> I18n