Lua Book

rzed rzantow at
Tue Mar 2 21:25:56 CET 2004

Pete Shinners <pete at> wrote in
news:mailman.28.1078240405.12614.python-list at 

> Cameron Laird wrote:
>> Incidentally, have you heard the news about Lua?  Look at
>> <URL: >.
> I have no experience with Lua, but some of the code examples
> from the book may frighten me away for good.
> I suppose Python's recent introduction of generators makes this
> rather trivial. 
> def allwords():
>      for line in sys.stdin:
>          for word in line.split():
>              yield word
> for word in allwords():
>      print word

NameError: global name 'sys' is not defined

The Python version includes trailing punctuation, while the Lua 
version filters it out. It becomes a little less trivial to make 
the two functionally identical. 

It seems that the syntax of Lua can be a little more verbose. On 
the other hand, here are two versions of (more or less) the 
functional equivalent of the referenced Lua code:

# in Python:

import sys, string
tt = string.maketrans( '`~!@#$%^&*()_-+=:;"\'{[}]|\\?/>.<,',
'                                ' )
for line in sys.stdin:
    line = string.translate( line, tt )
    for word in line.split():
        print word

-- in Lua:

line =
while line do
    for word in string.gfind(line, "%w+") do
    line =

There are probably better ways to write the code in either 
language. I've only been looking at Lua for a couple of hours, so I 
can't really comment much about that. But overall, for a comparable 
task, it doesn't look too scary.


More information about the Python-list mailing list