Module:Sandbox/Miria~01/sumMedals

local p = {}

function p.calculateSum(frame) local args = frame.args local targetClass = args[1] or '' -- Moved targetClass to the first argument local column = args[2] local wikitext = mw.title.new(args[3]):getContent local sum = 0 local sumGold = 0 local sumSilver = 0 local sumBronze = 0 local sumTotal = 0

-- Define a pattern to capture numbers enclosed in triple apostrophes or unformatted numbers local numPattern = "|%s*([%s']*%d+[%s']*)%s*|" local trimmedRow = "" -- Iterate through the rows of the table for row in mw.text.gsplit(wikitext, "\n") do       -- Check if the row contains the specified class if mw.ustring.find(row, 'class="' .. targetClass .. '"') then trimmedRow = row:match('.*class="' .. targetClass .. '"(.*)') -- Extract everything after the class attribute -- Extract the value from the specified column local i = 0 for value in mw.ustring.gmatch(trimmedRow, numPattern) do               i = i + 1 if value then -- Remove triple apostrophes, but treat as a string local stringValue = value:gsub("['%s]", "") if i == 1 then sumGold = sumGold + tonumber(stringValue) end if i == 2 then sumSilver = sumSilver + tonumber(stringValue) end if i == 3 then sumBronze = sumBronze + tonumber(stringValue) end if i == 4 then sumTotal = sumTotal + tonumber(stringValue) end end end end end

if column == "Gold" then return sumGold end if column == "Silver" then return sumSilver end if column == "Bronze" then return sumBronze end if column == "Total" then return sumTotal end if column == "All" then return tostring(sumGold) .. '||' .. tostring(sumSilver) .. '||' .. tostring(sumBronze) .. '||' .. tostring(sumTotal) end

return tostring(sum) end

return p