Difference between revisions of "Module:TableTools"

Jump to navigation Jump to search
790 bytes added ,  12:43, 15 December 2013
add getAffixNums function
>Mr. Stradivarius
(finish sparseIpairs)
>Mr. Stradivarius
(add getAffixNums function)
Line 39: Line 39:
if isPositiveInteger(k) then
if isPositiveInteger(k) then
nums[#nums + 1] = k
nums[#nums + 1] = k
end
end
table.sort(nums)
return nums
end
--[[
-----------------------------------------------------------------------------------
-- getAffixNums
--
-- This takes a table and returns an array containing the numbers of keys with the
-- specified prefix and suffix. For example, for the table
-- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", getAffixNums will
-- return {1, 3, 6}.
-----------------------------------------------------------------------------------
--]]
function p.getAffixNums(t, prefix, suffix)
prefix = prefix or ''
suffix = suffix or ''
local nums = {}
for k, v in pairs(t) do
if type(k) == 'string' then
local num = mw.ustring.match(k, '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$')
if num then
nums[#nums + 1] = tonumber(num)
end
end
end
end
end
Anonymous user

Navigation menu