[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: split (was: A citation on Lua)
- From: Luis Carvalho <carvalho@...>
- Date: Tue, 19 Dec 2006 07:11:17 -0500
> Here is a version using a light wrapper aroung string.gmatch. Because of
> the way gmatch is implemented (I think that was the reason exposed on
> this list when the issue has been raised), it works better if you
> concatenate a delimiter at the end of the string to split. But if the
> delimiter contains magic characters, you have to provide a simpler
> version that match the magic characters (term in the example below, for
> terminator). Tested with stock Lua 5.1:
Just to add my $.02, here is a terminator-free version. It should be slower
then Jerome's version, of course, but the convenience of using a magic
delimiter without the terminator might pay off if there's no need to split too
many lines. :)
local str1 = "foo bar baf"
local str2 = "foo<space type='a'>bar<space type='b'>baf"
function split(s, d)
local d = d or "%s"
splitaux = function(s)
local x, y = string.find(s, d)
if not x then
coroutine.yield(string.sub(s, 1, x-1))
return splitaux(string.sub(s, y+1))
return coroutine.wrap(function() return splitaux(s) end)
for token in split(str1) do
for token in split(str2, "<space[^>]*>") do
A mathematician is a device for turning coffee into theorems.
-- P. Erdos
Applied Math PhD Student - Brown University
PGP Key: E820854A <email@example.com>