Module:ModuleLevelTable: Difference between revisions

From The First Descendant Wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


function p.loadJson(frame)
function p.parseCsv(frame)
     -- Get the JSON string from the template parameter named 'json'
     -- Get the CSV string from the template parameter named 'csv'
     local jsonString = frame.args.json
     local csvString = frame.args.csv


     -- Parse the JSON string into a Lua table
     -- Initialize an empty multidimensional array
     local success, jsonTable = pcall(mw.text.jsonDecode, jsonString)
     local csvArray = {}


     if success then
     -- Split the CSV string into lines
         return jsonTable
    for line in csvString:gmatch("[^\n]+") do
    else
         local row = {}
         return "Error parsing JSON"
        -- Split each line into values using a comma as the delimiter
         for value in line:gmatch("[^,]+") do
            table.insert(row, value)
        end
        table.insert(csvArray, row)
     end
     end
    return csvArray
end
end


return p
return p

Revision as of 02:58, 4 June 2024

Documentation for this module may be created at Module:ModuleLevelTable/doc

local p = {}

function p.parseCsv(frame)
    -- Get the CSV string from the template parameter named 'csv'
    local csvString = frame.args.csv

    -- Initialize an empty multidimensional array
    local csvArray = {}

    -- Split the CSV string into lines
    for line in csvString:gmatch("[^\n]+") do
        local row = {}
        -- Split each line into values using a comma as the delimiter
        for value in line:gmatch("[^,]+") do
            table.insert(row, value)
        end
        table.insert(csvArray, row)
    end

    return csvArray
end

return p