lua-users home
lua-l archive

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


On 12/18/06, dcharno <dcharno@comcast.net> wrote:
> I think for him lack of 'split' counts as a design flaw?

Couldn't he just use gmatch ...

    function strsplit(s, delim)
      return string.gmatch(s, string.format("[^s]+", delim))
    end


I would presume you really meant something like this

function strsplit(s, delim)
   local t= {}
   for x in string.gmatch(s, string.format("[^%s]+", delim)) do
        t[#t+1]= x
   end
   return t
end

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".

It would be great if someone on this list could show a short and
convenient idiom for  "split" operation on strings.

--Leo--