[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Optimizing Csv to Field Table
- From: Billy <spooky_loco_pv@...>
- Date: Mon, 17 Dec 2012 01:31:03 +0000 (UTC)
Anyone know how i can optimize this? Or if i even wrote this correctly. I wanted
to create a function that would return the table corresponding with the fields
that the csv was created
fromCSV function can be found at http://lua-users.org/wiki/CsvUtils
-- Converts CSV file to a Field Table
-- example
-- Name,Class,FormID
-- Warrior,CombatWarrior1H,0005003
-- Archer,CombatArcher,0005002
-- returns {
-- [1] = { Name = Warrior, Class = CombatWarrior1H, FormID = 0005003 },
-- [2] = { Name = Archer, Class = CombatArcher, FormID = 0005002}
-- }
function CSVToTable(file_in)
local t = {}
local lineCount = 1
local count = 1
local fieldCount = 0
local fields = {}
for line in io.lines(file_in) do
local csv = fromCSV(line)
if lineCount == 1 then
fieldCount = #csv
if fieldCount == 0 then -- incase table size is nil,
lets recount
for k,v in pairs(csv) do
fieldCount = fieldCount + 1
end
else
for k,v in pairs(csv) do
fields[k] = Trim(v)
end
end
else
if not t[count] then
t[count] = {}
end
for k,v in pairs(csv) do
t[count][fields[k]] = csv[k]
end
count = count + 1
end
lineCount = lineCount + 1
end
return t
end