iterator

function iterator (state)

while state.line do -- repeat while there are lines

-- search for next word

local s, e = string.find(state.line, "%w+", state.pos)

if s then -- found a word?

-- update next position (after this word)

state.pos = e + 1

return string.sub(state.line, s, e)

else -- word not found

state.line = io.read() -- try next line...

state.pos = 1 -- ... from first position

end

end

return nil -- no more lines: end loop

end

你可能感兴趣的:(iterator)