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.parseCsv(frame)
function p.parseCSV(input)
     -- Get the CSV string from the template parameter named 'csv'
     local array = {}
     local csvString = frame.args[1]
     local i = 1
 
      
     -- Initialize an empty multidimensional array
     -- Split the input by lines
    local csvArray = {}
     for line in mw.text.split(input, "\n") do
 
         array[i] = {}
     -- Split the CSV string into lines
        local j = 1
     for line in csvString:gmatch("[^\n]+") do
       
         local row = {}
         -- Split each line by commas
         -- Split each line into values using a comma as the delimiter
         for cell in mw.text.split(line, ",") do
         for value in line:gmatch("[^,]+") do
             array[i][j] = mw.text.trim(cell)
             table.insert(row, value)
            j = j + 1
         end
         end
         table.insert(csvArray, row)
          
        i = i + 1
     end
     end
   
    return array
end


     mw.logObject(csvArray[1][1])
function p.getTable(input)
     return csvArray[1][1]
     local array = p.parseCSV(input)
     return array[1][1]
end
end


return p
return p

Revision as of 03:18, 4 June 2024

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

local p = {}

function p.parseCSV(input)
    local array = {}
    local i = 1
    
    -- Split the input by lines
    for line in mw.text.split(input, "\n") do
        array[i] = {}
        local j = 1
        
        -- Split each line by commas
        for cell in mw.text.split(line, ",") do
            array[i][j] = mw.text.trim(cell)
            j = j + 1
        end
        
        i = i + 1
    end
    
    return array
end

function p.getTable(input)
    local array = p.parseCSV(input)
    return array[1][1]
end

return p