Jump to content

Module:TableTools: Difference between revisions

add getAffixNums function
m>Mr. Stradivarius
(finish sparseIpairs)
m>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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.