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

32
LINES

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

Lua code

1  
list = {"hello world"}
2  
idx = 0
3  
4  
io = {}
5  
io.read = function()
6  
  idx = idx+1
7  
  return list[idx]
8  
end
9  
10  
function allwords ()
11  
  local line = io.read()  -- current line
12  
  local pos = 1           -- current position in the line
13  
  return function ()      -- iterator function
14  
    while line do         -- repeat while there are lines
15  
      local s, e = string.find(line, "%w+", pos)
16  
      if s then           -- found a word?
17  
        pos = e + 1       -- next position is after this word
18  
        return string.sub(line, s, e)     -- return the word
19  
      else
20  
        line = io.read()  -- word not found; try next line
21  
        pos = 1           -- restart from first position
22  
      end
23  
    end
24  
    return nil            -- no more lines: end of traversal
25  
  end
26  
end
27  
28  
29  
-- use example
30  
for word in allwords() do
31  
  print(word)
32  
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: 762 / 171
Referenced in: [show references]