http://proplan.55bet-pro.com/wiki/index.php?action=history&feed=atom&title=M%C3%B3dulo%3ATableTools Módulo:TableTools - Histórico de revisão-55BET Pro 2026-03-13T18:43:26Z Histórico de revisões para esta página neste wiki MediaWiki 1.42.7 http://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:TableTools&diff=79&oldid=prev Módulo:TableTools - Histórico de revisão-55BET Pro 2024-06-27T11:11:54Z <p>uma edição</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <tr class="diff-title" lang="pt-BR"> <td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Edição anterior</td> <td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Edição das 08h11min de 27 de junho de 2024</td> </tr><tr><td colspan="2" class="diff-notice" lang="pt-BR"><div class="mw-diff-empty">(Sem diferença)</div> </td></tr></table> Henryzord http://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:TableTools&diff=78&oldid=prev Módulo:TableTools - Histórico de revisão-55BET Pro 2024-04-27T01:49:03Z <p>Update from sandbox per request</p> <p><b>Página nova</b></p><div>------------------------------------------------------------------------------------<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 not --<br /> -- be called directly from #invoke. --<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 /> local checkTypeMulti = libraryUtil.checkTypeMulti<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 /> function p.isPositiveInteger(v)<br /> return type(v) == &#039;number&#039; and v &gt;= 1 and floor(v) == v and v &lt; infinity<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- isNan<br /> --<br /> -- This function returns true if the given number is a NaN value, and false if<br /> -- not. Although it doesn&#039;t operate on tables, it is included here as it is useful<br /> -- for determining whether a value can be a valid table key. Lua will generate an<br /> -- error if a NaN is used as a table key.<br /> ------------------------------------------------------------------------------------<br /> function p.isNan(v)<br /> return type(v) == &#039;number&#039; and v ~= v<br /> end<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 /> function p.shallowClone(t)<br /> checkType(&#039;shallowClone&#039;, 1, t, &#039;table&#039;)<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 /> -- 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 /> function p.removeDuplicates(arr)<br /> checkType(&#039;removeDuplicates&#039;, 1, arr, &#039;table&#039;)<br /> local isNan = p.isNan<br /> local ret, exists = {}, {}<br /> for _, v in ipairs(arr) 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 /> elseif not exists[v] then<br /> ret[#ret + 1] = v<br /> exists[v] = true<br /> end<br /> end<br /> return ret<br /> end<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 /> 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 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 /> -- 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 return<br /> -- {1, 3, 6}.<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 /> return s:gsub(&#039;([%(%)%%%.%[%]%*%+%-%?%^%$])&#039;, &#039;%%%1&#039;)<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 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 /> -- 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;. The<br /> -- compress option compresses the table so that it can be iterated over with<br /> -- ipairs.<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 /> -- 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 /> 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 /> -- sparseIpairs<br /> --<br /> -- This is an iterator for sparse arrays. It can be used like ipairs, but can<br /> -- handle nil values.<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 /> -- 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 /> function p.size(t)<br /> checkType(&#039;size&#039;, 1, t, &#039;table&#039;)<br /> local i = 0<br /> for _ in pairs(t) do<br /> i = i + 1<br /> end<br /> return i<br /> end<br /> <br /> local function defaultKeySort(item1, item2)<br /> -- &quot;number&quot; &lt; &quot;string&quot;, so numbers will be sorted before strings.<br /> local type1, type2 = type(item1), type(item2)<br /> if type1 ~= type2 then<br /> return type1 &lt; type2<br /> elseif type1 == &#039;table&#039; or type1 == &#039;boolean&#039; or type1 == &#039;function&#039; then<br /> return tostring(item1) &lt; tostring(item2)<br /> else<br /> return item1 &lt; item2<br /> end<br /> end<br /> ------------------------------------------------------------------------------------<br /> -- keysToList<br /> --<br /> -- Returns an array of the keys in a table, sorted using either a default<br /> -- comparison function or a custom keySort function.<br /> ------------------------------------------------------------------------------------<br /> function p.keysToList(t, keySort, checked)<br /> if not checked then<br /> checkType(&#039;keysToList&#039;, 1, t, &#039;table&#039;)<br /> checkTypeMulti(&#039;keysToList&#039;, 2, keySort, {&#039;function&#039;, &#039;boolean&#039;, &#039;nil&#039;})<br /> end<br /> <br /> local arr = {}<br /> local index = 1<br /> for k in pairs(t) do<br /> arr[index] = k<br /> index = index + 1<br /> end<br /> <br /> if keySort ~= false then<br /> keySort = type(keySort) == &#039;function&#039; and keySort or defaultKeySort<br /> table.sort(arr, keySort)<br /> end<br /> <br /> return arr<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- sortedPairs<br /> --<br /> -- Iterates through a table, with the keys sorted using the keysToList function.<br /> -- If there are only numerical keys, sparseIpairs is probably more efficient.<br /> ------------------------------------------------------------------------------------<br /> function p.sortedPairs(t, keySort)<br /> checkType(&#039;sortedPairs&#039;, 1, t, &#039;table&#039;)<br /> checkType(&#039;sortedPairs&#039;, 2, keySort, &#039;function&#039;, true)<br /> <br /> local arr = p.keysToList(t, keySort, true)<br /> <br /> local i = 0<br /> return function ()<br /> i = i + 1<br /> local key = arr[i]<br /> if key ~= nil then<br /> return key, t[key]<br /> else<br /> return nil, nil<br /> end<br /> end<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- isArray<br /> --<br /> -- Returns true if the given value is a table and all keys are consecutive<br /> -- integers starting at 1.<br /> ------------------------------------------------------------------------------------<br /> function p.isArray(v)<br /> if type(v) ~= &#039;table&#039; then<br /> return false<br /> end<br /> local i = 0<br /> for _ in pairs(v) do<br /> i = i + 1<br /> if v[i] == nil then<br /> return false<br /> end<br /> end<br /> return true<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- isArrayLike<br /> --<br /> -- Returns true if the given value is iterable and all keys are consecutive<br /> -- integers starting at 1.<br /> ------------------------------------------------------------------------------------<br /> function p.isArrayLike(v)<br /> if not pcall(pairs, v) then<br /> return false<br /> end<br /> local i = 0<br /> for _ in pairs(v) do<br /> i = i + 1<br /> if v[i] == nil then<br /> return false<br /> end<br /> end<br /> return true<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- invert<br /> --<br /> -- Transposes the keys and values in an array. For example, {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;} -&gt;<br /> -- {a = 1, b = 2, c = 3}. Duplicates are not supported (result values refer to<br /> -- the index of the last duplicate) and NaN values are ignored.<br /> ------------------------------------------------------------------------------------<br /> function p.invert(arr)<br /> checkType(&quot;invert&quot;, 1, arr, &quot;table&quot;)<br /> local isNan = p.isNan<br /> local map = {}<br /> for i, v in ipairs(arr) do<br /> if not isNan(v) then<br /> map[v] = i<br /> end<br /> end<br /> <br /> return map<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- listToSet<br /> --<br /> -- Creates a set from the array part of the table. Indexing the set by any of the<br /> -- values of the array returns true. For example, {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;} -&gt;<br /> -- {a = true, b = true, c = true}. NaN values are ignored as Lua considers them<br /> -- never equal to any value (including other NaNs or even themselves).<br /> ------------------------------------------------------------------------------------<br /> function p.listToSet(arr)<br /> checkType(&quot;listToSet&quot;, 1, arr, &quot;table&quot;)<br /> local isNan = p.isNan<br /> local set = {}<br /> for _, v in ipairs(arr) do<br /> if not isNan(v) then<br /> set[v] = true<br /> end<br /> end<br /> <br /> return set<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- deepCopy<br /> --<br /> -- Recursive deep copy function. Preserves identities of subtables.<br /> ------------------------------------------------------------------------------------<br /> local function _deepCopy(orig, includeMetatable, already_seen)<br /> if type(orig) ~= &quot;table&quot; then<br /> return orig<br /> end<br /> <br /> -- already_seen stores copies of tables indexed by the original table.<br /> local copy = already_seen[orig]<br /> if copy ~= nil then<br /> return copy<br /> end<br /> <br /> copy = {}<br /> already_seen[orig] = copy -- memoize before any recursion, to avoid infinite loops<br /> <br /> for orig_key, orig_value in pairs(orig) do<br /> copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen)<br /> end<br /> <br /> if includeMetatable then<br /> local mt = getmetatable(orig)<br /> if mt ~= nil then<br /> setmetatable(copy, _deepCopy(mt, true, already_seen))<br /> end<br /> end<br /> <br /> return copy<br /> end<br /> <br /> function p.deepCopy(orig, noMetatable, already_seen)<br /> checkType(&quot;deepCopy&quot;, 3, already_seen, &quot;table&quot;, true)<br /> return _deepCopy(orig, not noMetatable, already_seen or {})<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- sparseConcat<br /> --<br /> -- Concatenates all values in the table that are indexed by a number, in order.<br /> -- sparseConcat{a, nil, c, d} =&gt; &quot;acd&quot;<br /> -- sparseConcat{nil, b, c, d} =&gt; &quot;bcd&quot;<br /> ------------------------------------------------------------------------------------<br /> function p.sparseConcat(t, sep, i, j)<br /> local arr = {}<br /> <br /> local arr_i = 0<br /> for _, v in p.sparseIpairs(t) do<br /> arr_i = arr_i + 1<br /> arr[arr_i] = v<br /> end<br /> <br /> return table.concat(arr, sep, i, j)<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- length<br /> --<br /> -- Finds the length of an array, or of a quasi-array with keys such as &quot;data1&quot;,<br /> -- &quot;data2&quot;, etc., using an exponential search algorithm. It is similar to the<br /> -- operator #, but may return a different value when there are gaps in the array<br /> -- portion of the table. Intended to be used on data loaded with mw.loadData. For<br /> -- other tables, use #.<br /> -- Note: #frame.args in frame object always be set to 0, regardless of the number<br /> -- of unnamed template parameters, so use this function for frame.args.<br /> ------------------------------------------------------------------------------------<br /> function p.length(t, prefix)<br /> -- requiring module inline so that [[Module:Exponential search]] which is<br /> -- only needed by this one function doesn&#039;t get millions of transclusions<br /> local expSearch = require(&quot;Module:Exponential search&quot;)<br /> checkType(&#039;length&#039;, 1, t, &#039;table&#039;)<br /> checkType(&#039;length&#039;, 2, prefix, &#039;string&#039;, true)<br /> return expSearch(function (i)<br /> local key<br /> if prefix then<br /> key = prefix .. tostring(i)<br /> else<br /> key = i<br /> end<br /> return t[key] ~= nil<br /> end) or 0<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- inArray<br /> --<br /> -- Returns true if valueToFind is a member of the array, and false otherwise.<br /> ------------------------------------------------------------------------------------<br /> function p.inArray(arr, valueToFind)<br /> checkType(&quot;inArray&quot;, 1, arr, &quot;table&quot;)<br /> -- if valueToFind is nil, error?<br /> <br /> for _, v in ipairs(arr) do<br /> if v == valueToFind then<br /> return true<br /> end<br /> end<br /> return false<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- merge<br /> --<br /> -- Given the arrays, returns an array containing the elements of each input array<br /> -- in sequence.<br /> ------------------------------------------------------------------------------------<br /> function p.merge(...)<br /> local arrays = {...}<br /> local ret = {}<br /> for i, arr in ipairs(arrays) do<br /> checkType(&#039;merge&#039;, i, arr, &#039;table&#039;)<br /> for _, v in ipairs(arr) do<br /> ret[#ret + 1] = v<br /> end<br /> end<br /> return ret<br /> end<br /> <br /> ------------------------------------------------------------------------------------<br /> -- extend<br /> --<br /> -- Extends the first array in place by appending all elements from the second<br /> -- array.<br /> ------------------------------------------------------------------------------------<br /> function p.extend(arr1, arr2)<br /> checkType(&#039;extend&#039;, 1, arr1, &#039;table&#039;)<br /> checkType(&#039;extend&#039;, 2, arr2, &#039;table&#039;)<br /> <br /> for _, v in ipairs(arr2) do<br /> arr1[#arr1 + 1] = v<br /> end<br /> end<br /> <br /> return p</div> infobox>Pppery