lua-users home
lua-l archive

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

Leo Razoumov wrote:
In any case it is NOT equivalent to a split operation (Ruby, Pearl)
that splits on a matching pattern. To see the difference, choose a
delimiter to be a string of several characters like delim="SEP". The
strsplit() implementation above will skip all occurrences of letters
S,E,P anywhere in the string which is very different from splitting on
a single string "SEP".

You're right. I rarely split on anything other than commas or semicolons. Here is a version that seems to handle the perl examples I could find and was a little shorter than other samples I saw:

   function strsplit(s, delim)
     local t = {}
     local pat = "(.-)" .. delim .. "(.+)"
     local c1, c2
       c1, c2 = string.match(s, pat)
       t[#t+1] = c1 or s
       s = c2
     until c1 == nil
     return t

Not that this whole thing was really about string splitting anyways ...