Module:ArrayList: Difference between revisions
Appearance
Content deleted Content added
delimiter fixes |
another try |
||
Line 17: | Line 17: | ||
return p[func](frame) |
return p[func](frame) |
||
else |
else |
||
return "void: |
return "void:notfound " .. tostring(func) |
||
end |
end |
||
end |
end |
||
Line 26: | Line 26: | ||
local delimiter = frame.args[3] or "," |
local delimiter = frame.args[3] or "," |
||
delimiter = escapePattern(delimiter) |
delimiter = escapePattern(delimiter) |
||
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
||
local count = select(2, str:gsub(delimiter, "")) |
local count = select(2, str:gsub(delimiter, "")) |
||
Line 38: | Line 37: | ||
local index = frame.args[4] |
local index = frame.args[4] |
||
delimiter = escapePattern(delimiter) |
delimiter = escapePattern(delimiter) |
||
-- Remove leading and trailing delimiters and whitespace |
|||
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
||
local items = {} |
local items = {} |
||
for item in string.gmatch(str, "([^" .. delimiter .. "]+)") do |
for item in string.gmatch(str, "([^" .. delimiter .. "]+)") do |
||
table.insert(items, trim(item)) |
table.insert(items, trim(item)) |
||
end |
end |
||
if index == "last" then |
if index == "last" then |
||
index = #items |
index = #items |
||
Line 57: | Line 52: | ||
return "void:invalid" |
return "void:invalid" |
||
end |
end |
||
⚫ | |||
⚫ | |||
end |
end |
||
Line 68: | Line 62: | ||
local occurrence = tonumber(frame.args[5]) |
local occurrence = tonumber(frame.args[5]) |
||
delimiter = escapePattern(delimiter) |
delimiter = escapePattern(delimiter) |
||
-- Remove leading and trailing delimiters and whitespace |
|||
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
||
local positions = {} |
local positions = {} |
||
local index = 1 |
local index = 1 |
||
-- Iterate over items split by the delimiter |
|||
for subitem in string.gmatch(str, "([^" .. delimiter .. "]+)") do |
for subitem in string.gmatch(str, "([^" .. delimiter .. "]+)") do |
||
subitem = trim(subitem) |
subitem = trim(subitem) |
||
Line 83: | Line 72: | ||
index = index + 1 |
index = index + 1 |
||
end |
end |
||
if not occurrence then |
if not occurrence then |
||
return #positions > 0 and table.concat(positions, ",") or "void:nomatch" |
|||
return #positions > 0 and table.concat(positions, delimiter) or "void:nomatch" |
|||
else |
else |
||
-- Return the specified occurrence or -1 if not found |
|||
return positions[occurrence] or -1 |
return positions[occurrence] or -1 |
||
end |
end |
||
Line 99: | Line 85: | ||
local operation = frame.args[4] |
local operation = frame.args[4] |
||
delimiter = escapePattern(delimiter) |
delimiter = escapePattern(delimiter) |
||
-- Remove leading and trailing delimiters and whitespace |
|||
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1") |
||
local items = {} |
local items = {} |
||
for item in string.gmatch(str, "([^" .. delimiter .. "]+)") do |
for item in string.gmatch(str, "([^" .. delimiter .. "]+)") do |
||
Line 112: | Line 95: | ||
end |
end |
||
end |
end |
||
if #items == 0 then |
if #items == 0 then |
||
return |
return "void:nonumeric" |
||
⚫ | |||
end |
|||
⚫ | |||
local total = 0 |
local total = 0 |
||
for _, num in ipairs(items) do |
for _, num in ipairs(items) do |
Revision as of 14:06, 26 July 2024
local p = {}
-- Helper function to trim whitespace
local function trim(s)
return s:match("^%s*(.-)%s*$")
end
-- Escape special characters in the delimiter for pattern matching
local function escapePattern(str)
return str:gsub("([%^%$%(%)%%%.%[%]%*%+%?%-])", "%%%1")
end
-- Main function to call other functions dynamically
function p.main(frame)
local func = frame.args[1]
if p[func] then
return p[func](frame)
else
return "void:notfound " .. tostring(func)
end
end
-- Count occurrences of delimiter in a string
function p.count(frame)
local str = frame.args[2] or ""
local delimiter = frame.args[3] or ","
delimiter = escapePattern(delimiter)
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1")
local count = select(2, str:gsub(delimiter, ""))
return count + 1
end
-- Get the Nth item in a delimited string, supporting negative indices
function p.get(frame)
local str = frame.args[2] or ""
local delimiter = frame.args[3] or ","
local index = frame.args[4]
delimiter = escapePattern(delimiter)
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1")
local items = {}
for item in string.gmatch(str, "([^" .. delimiter .. "]+)") do
table.insert(items, trim(item))
end
if index == "last" then
index = #items
elseif index and tonumber(index) then
index = tonumber(index)
if index < 0 then
index = #items + index + 1
end
else
return "void:invalid"
end
return items[index] or "void:outrange"
end
-- Find the position of the Nth occurrence of a matching item in a delimited string
function p.pos(frame)
local str = frame.args[2] or ""
local delimiter = frame.args[3] or ","
local item = frame.args[4] or ""
local occurrence = tonumber(frame.args[5])
delimiter = escapePattern(delimiter)
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1")
local positions = {}
local index = 1
for subitem in string.gmatch(str, "([^" .. delimiter .. "]+)") do
subitem = trim(subitem)
if subitem == item then
table.insert(positions, index)
end
index = index + 1
end
if not occurrence then
return #positions > 0 and table.concat(positions, ",") or "void:nomatch"
else
return positions[occurrence] or -1
end
end
-- Perform mathematical operations on numeric array items
function p.math(frame)
local str = frame.args[2] or ""
local delimiter = frame.args[3] or ","
local operation = frame.args[4]
delimiter = escapePattern(delimiter)
str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1")
local items = {}
for item in string.gmatch(str, "([^" .. delimiter .. "]+)") do
local number = tonumber(trim(item))
if number then
table.insert(items, number)
else
return "void:isalpha"
end
end
if #items == 0 then
return "void:nonumeric"
elseif operation == "sum" then
local total = 0
for _, num in ipairs(items) do
total = total + num
end
return total
elseif operation == "min" then
local min = items[1]
for _, num in ipairs(items) do
if num < min then
min = num
end
end
return min
elseif operation == "max" then
local max = items[1]
for _, num in ipairs(items) do
if num > max then
max = num
end
end
return max
else
return "void:unsupported"
end
end
return p