Jump to content

Module:ArrayList

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Bungle (talk | contribs) at 11:36, 26 July 2024 (create lua module for array lists function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local p = {}

-- Helper function to trim whitespace
local function trim(s)
    return s:match("^%s*(.-)%s*$")
end

-- Count occurrences of delimiter in a string
function p.count(frame)
    local str = frame.args[2] or ""
    local delimiter = frame.args[3] or ","
    
    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]
    
    -- Remove leading and trailing delimiters and whitespace
    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 "Invalid index"
    end
    
    return items[index] or "Index out of range"
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])

    -- Remove leading and trailing delimiters and whitespace
    str = str:gsub("^%s*" .. delimiter .. "%s*(.-)%s*" .. delimiter .. "$", "%1")

    local positions = {}
    local index = 1

    -- Iterate over items split by the delimiter
    for subitem in string.gmatch(str, "([^" .. delimiter .. "]+)") do
        subitem = trim(subitem) -- Trim whitespace
        if subitem == item then
            table.insert(positions, index)
        end
        index = index + 1
    end

    if not occurrence then
        -- Return positions joined by the delimiter, if positions are found
        return #positions > 0 and table.concat(positions, delimiter) or "No match found"
    else
        -- Return the specified occurrence or -1 if not found
        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]
    
    -- Remove leading and trailing delimiters and whitespace
    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 null
    end
    
    if 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