Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

32
LINES

< > BotCompany Repo | #312 // allwords.lua

Lua code

list = {"hello world"}
idx = 0

io = {}
io.read = function()
  idx = idx+1
  return list[idx]
end

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       -- next position is after this word
        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


-- use example
for word in allwords() do
  print(word)
end

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 847 [visualize]

Snippet ID: #312
Snippet name: allwords.lua
Eternal ID of this version: #312/1
Text MD5: 64a4a6c3b6a7e4f94de0f2affd906d23
Author: meetup1
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-01-17 02:39:54
Source code size: 847 bytes / 32 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 760 / 171
Referenced in: [show references]