Monday, December 19, 2011

Learning Lua

So I have learned about LuaTeX, which basically implements TeX in Lua and thus enables Lua scripting inside a TeX document. Thus I want to learn Lua!

Hello World!

I installed the basic lua50 package on ubuntu, so let me write my first program.

-- begin hello.lua

-- This is a comment

print("Goodbye, Cruel World!")

-- end hello.lua

One runs it on the *nix command line by $ lua hello.lua

However, we can use write(...) instead of print(...), the difference is that print() automatically adds a new line.

Getting User Input

We can ask for user's information:

-- begin name.lua 

io.write("What's your name? ")
name = io.read()
print("Why, '"..name.."' is a stupid name!")

-- end name.lua

We start using the io module's functions. We need to indicate this by calling io.read() and io.write()

Unique Aspects of Lua

Lua is unique in that everything is-a table. That's how you implement classes, data structures, and so on. (Think how LISP has everything is-a list.)

Even operator overloading is handled through tables...well, "metatables".

There are many examples of advanced table usage on Lua's wiki. So next time, we'll start considering examples involving tables.

No comments: