lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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/CsvUtils

There 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