Module:Calculate sum in tables

-- Define the main function local p = {}

function p.calculateSum(frame) -- Get the arguments passed to the function local args = frame.args

-- Initialize a table to store the sums for each column local columnSums = {}

-- Define the columns to be calculated (e.g., 2 to 5) local startColumn = tonumber(args[1]) or 2 -- Default to 2 if not provided local endColumn = tonumber(args[2]) or 5 -- Default to 5 if not provided

-- Initialize the sums for each column to 0 for i = startColumn, endColumn do       columnSums[i] = 0 end

-- Loop through the rows of the table and update the sums for each column for rowIndex = 1, #args - 1 do -- Exclude the last row for i = startColumn, endColumn do           local cellValue = args[rowIndex][i] if type(cellValue) == "number" then columnSums[i] = columnSums[i] + cellValue elseif type(cellValue) == "string" then local num = tonumber(cellValue) if num then columnSums[i] = columnSums[i] + num end end end end

-- Create a string representation of the sums local sumString = "" for i = startColumn, endColumn do       sumString = sumString .. "||" .. columnSums[i] end

-- Return the sums as a string without leading "||" return sumString:sub(3) end

-- Return the module table return p