lua-users home
lua-l archive

Re: help with splitting strings

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


Depending on what you're doing, it might be better to use an iterator
with a generic for loop. The function you posted creates a table which
usually isn't desirable if the table is short lived.
If this would be useful:
for str in split(somestring, "pattern") do ... end
Then you might want to use this:
function split(str, delim)
 local oldf = 0;
 return function()
 local start;
 if oldf == -1 then return nil end;
 oldf, start = str:find(delim, oldf + 1) or -1, oldf;
 return str:sub(start + 1, oldf);
 end
end;
HTH,
-- 
-Patrick Donnelly
"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."
-Will Durant

AltStyle によって変換されたページ (->オリジナル) /