Lua tutorial help for Python programmer?

Andrea D'Amore and.damore at gmail.com
Tue Nov 8 03:12:30 EST 2016


On 7 November 2016 at 20:27, Skip Montanaro <skip.montanaro at gmail.com> wrote:
> I just got Lua scripting dumped in my lap as a way to do some server
> side scripting in Redis. The very most basic stuff isn't too hard (i =
> 1, a = {"x"=4, ...}, for i = 1,10,2 do ... end), but as soon as I get
> beyond that, I find it difficult to formulate questions which coax
> Google into useful suggestions. Is there an equivalent to the
> python-tutor, python-help, or even this (python-list/comp.lang.python)
> for people to ask Lua questions from the perspective of a Python
> programmer? Maybe an idiom translation table?

There's lua-list, I figure all your questions fit better there than here.
There's the official wiki on lua-users [1], I don't know about a
Py-Lua Rosetta Stone.


> 1. print(tbl) where tbl is a Lua table prints something useless like
[…]
> How can I print a table in one go so I see all its keys and values?

Use the pairs() iterator function (check the reference manual for
ipairs() as well):

    for key, value in pairs(my_table) do
        print(key, value)
    end


> 2. The redis-py package helpfully converts the result of HGETALL to a
> Python dictionary. On the server, The Lua code just sees an
> interleaved list (array?) of the key/value pairs, e.g., "a" "1" "b"
> "2" "c" "hello". I'd dictify that in Python easily enough:
[…]
> Skimming the Lua reference manual, I didn't see anything like dict()
> and zip().

IIRC tables are the only data structures in Lua, actually I liked this
simplification very much.


> I suspect I'm thinking like a Python programmer when I
> shouldn't be. Is there a Lua idiom which tackles this problem in a
> straightforward manner, short of a numeric for loop?


IIRC the standard library is quite compact, so no.
If you want something like more straightforward than

    dict = {}
    for i = 1, #results, 2 do
        dict[results[i]] = results[i+1]
    end

You can define your own iterator function and have "for key, value in
…" in the for loop. Not sure it's worth it.

Beware that I'm no Lua expert, I just liked the language and read
about it but never actually used in any project. I suggesting checking
the mailing list or the IRC channel.


> As you can see, this is pretty trivial stuff, mostly representing
> things which are just above the level of the simplest tutorial.

Check "Programming in Lua" book, older versions are made available
online by the author.


[1]: http://lua-users.org/wiki/TutorialDirectory

-- 
Andrea



More information about the Python-list mailing list