Lua Book

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


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

> Cameron Laird wrote:
>> Incidentally, have you heard the news about Lua?  Look at
>> <URL: http://www.inf.puc-rio.br/~roberto/book/ >.
> 
> I have no experience with Lua, but some of the code examples
> from the book may frighten me away for good.
> 
> http://www.inf.puc-rio.br/~roberto/book/code/allwords.lua.html
> 
> 
> 
> 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 = io.read()
while line do
    for word in string.gfind(line, "%w+") do
        print(word)
    end
    line = io.read()
end

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.

-- 
rzed




More information about the Python-list mailing list