lua-users home
lua-l archive

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


Hmmm,

When I first read this message, I thought "well sure you can break up
something simple like a comma delimited list!"

I was wrong.

The problem is, as you say in your e-mail message, empty fields, or more
exactly pairs of commas right next to each other. The best solution I've
been able to find misses the last data element, and so requires a
separate search for that:

   -- Assume the variable "line" is the string
   t = {}
   function tadd(x) table.insert(t,x) end
   string.gsub(line, "(.-)%,", tadd)
   string.gsub(line, "([^%,]*)$",t.add)
   table.remove(t) -- get rid of extra field at end

Of course you can also insert a space between pairs of commas before
doing the initial gsub if your application allows for that:

    ...
    line = string.gsub(line, "%,%,", ", ,")
    string.gsub(line, "([^%,]+)", t.add)

but this is not a general solution. Actually your "add the extra comma
at the end" is general and easier to do, if not as elegant as I'd like.

Oh well, I've always thought of regular expressions as a  strictly
practical tool anyway. Elegance is nice, but not required.

   - Tom Wrensch

>>> w.couwenberg@chello.nl 04/25/03 16:30 PM >>>
I've regularly come across another issue while using gsub (Lua 4), but I
guess its also there for gfind in Lua 5.  It seems to be impossible to
write
a single pattern that splits a string into its comma-seperated parts,
allowing for empty parts.  (Try it, it can be puzzling!)  The workaround
I
use most of the times is to append an additional comma to the string
first.
That's not too bad, but still...  A possible solution would be to allow
the
"end-of-string" character $ inside a character set without losing its
special meaning.  Then the pattern could tentatively be "(.-)[,$]".  I'm
not
sure how other regex systems handle this or how difficult it would be to
support it in the Lua implementation (which performs excellently by the
way!)  Any thoughts on this one??

Bye,
Wim