local p =
-- Helper function to trim whitespacelocal function trim(s) return s:match("^%s*(.-)%s*$")end
-- Escape special characters in the delimiter for pattern matchinglocal function escapePattern(str) return str:gsub("([%^%$%(%)%%%.%[%]%*%+%?%-])", "%%%1")end
-- Main function to call other functions dynamicallyfunction p.main(frame) local func = frame.args[1] if p[func] then return p[func](frame) else return "void:notfound " .. tostring(func) endend
-- Count occurrences of delimiter in a stringfunction 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 + 1end
-- Get the Nth item in a delimited string, supporting negative indicesfunction 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
-- Find the position of the Nth occurrence of a matching item in a delimited stringfunction 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
-- Perform mathematical operations on numeric array itemsfunction 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
"sum" then local total = 0 for _, num in ipairs(items) do total = total + num end return total 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" endend
return p