Advertisement
igovasconcelos

Untitled

Aug 31st, 2020
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.64 KB | None | 0 0
  1. function allwords ()
  2.   local line = io.read()    -- current line
  3.   local pos = 1             -- current position in the line
  4.   return function ()        -- iterator function
  5.     while line do           -- repeat while there are lines
  6.       local s, e = string.find(line, "%w+", pos)
  7.       if s then      -- found a word?
  8.         pos = e + 1  -- update next position
  9.         return string.sub(line, s, e)   -- return the word
  10.       else
  11.         line = io.read()    -- word not found; try next line
  12.         pos = 1             -- restart from first position
  13.       end
  14.     end
  15.     return nil            -- no more lines: end of traversal
  16.   end
  17. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement