Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function allwords ()
- local line = io.read() -- current line
- local pos = 1 -- current position in the line
- return function () -- iterator function
- while line do -- repeat while there are lines
- local s, e = string.find(line, "%w+", pos)
- if s then -- found a word?
- pos = e + 1 -- update next position
- return string.sub(line, s, e) -- return the word
- else
- line = io.read() -- word not found; try next line
- pos = 1 -- restart from first position
- end
- end
- return nil -- no more lines: end of traversal
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement