Module:Template input parse: Difference between revisions
No edit summary |
Added variant function that returns split numeric values |
||
Line 13: | Line 13: | ||
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1") | local trimmed = entry:gsub("^%s*(.-)%s*$", "%1") | ||
table.insert(parsed, trimmed) -- Add to array | table.insert(parsed, trimmed) -- Add to array | ||
end | |||
return parsed | |||
end | |||
-- Helper function | |||
-- Parses entries from a delimited string and returns them in an array | |||
-- Values entered are convereted into numbers rather than strings. | |||
-- Default delimiter is semicolon (;) | |||
function p.parse_numeric_entries(unparsed, delimiter) | |||
local parsed = {} | |||
local delimiter = delimiter or ';' | |||
local expr = '([^' .. delimiter .. ']+)' | |||
for entry in string.gmatch(unparsed, expr) do | |||
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1") | |||
local numeric = tonumber(trimmed) | |||
table.insert(parsed, numeric) -- Add to array | |||
end | end | ||
return parsed | return parsed |