# Optimizing Csv to Field Table

5 messages
Open this post in threaded view
|

## Optimizing Csv to Field Table

 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
Open this post in threaded view
|

## Re: Optimizing Csv to Field Table

 On 12/16/2012 08:31 PM, Billy wrote: > 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/CsvUtilsThere are a lot of things that you could do to optimize this function (and simplify it), but I'd guess that the single best thing you could do is to just make a modified version of fromCSV() that directly constructs your table, rather than using its output table to construct another one. That said, if you want to simplify your function, a few things off the top of my head: * Get rid of linecount and just read the first line outside the loop * Get rid of that "let's recount" code and just abort (or continue) if fieldcount==0 * Replace all of pairs() with ipairs() * Get rid of count and references to t[count] (just construct each new record as a local table, then append it to t, e.g. with table.insert) * You need some kind of handling of the case where the number of fields in a detail line doesn't match the number of fields in the header, especially if it's larger. -- David
Open this post in threaded view
|