lua-users home
lua-l archive

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


>Any thoughts on making a function that's the opposite as well? Something
>that would take a string and a seperator and return a table whose elements
>contained the original string split apart based on the separator. Or can
>this be done with gsub without creating a bunch of temp strings? I'm not

How about this?

function split(s,t)
 local a={}
 gsub(s,"(.-)"..t,function (x) tinsert(%a,x) end)
end

(This assumes that the separator t occurs at the end of the string s too.)

gsub only creates the the strings for the captures and the string that is
returned (in this cases, the returned string is ignored).
--lhf