The following is a very brief and incomplete overview of Lua's syntax and some of its quirks. This is mostly meant for programmers already familiar with at least one other programming language, but unfamiliar with Lua.
If you want to know more about Lua, any of the following links is a good start:
Variables
4 -- Local variables are preferred. They keep the global scope clean
5 -- and are a bit faster for access.
6 local localVariable = 42
8 -- Undefined variables are nil by default
9 assert(idontexist == nil)
Tables
4 ["Key with spaces"] = "anotherValue", -- Note the (optional) trailing comma
7 -- The following two lines are equivalent
9 myTable["a"] = "Thrive"
11 -- To remove an item, assign nil:
14 -- Arrays are just tables with numeric keys
24 -- Indices start at 1, not 0
25 assert(myArray[1] == "first")
27 -- Accessing non-existant keys gives back nil
28 assert(myArray[0] == nil)
29 assert(myTable.nosuchkey == nil)
Functions and Classes
1 function myFunction(x, y)
5 -- Functions, just like variables, should be declared local where possible
6 local function myLocalFunction(x, y)
10 -- Functions without a return implicitly return nil
11 local function returnsNil()
13 assert(returnsNil() == nil)
15 -- Classes are not a standard Lua feature, but the luabind library
16 -- enables them for us
20 function MyClass:__init(x)
24 function MyClass:doSomething()
25 print("My x is " .. tostring(self.x))
28 local myObject = MyClass(5)
29 assert(myObject.x == 5)
31 -- A word about this strange colon: The following three lines are equivalent
32 myObject:doSomething()
33 myObject.doSomething(myObject)
34 MyClass.doSomething(myObject)
36 -- As are these two definitions
37 function MyClass:doMore()
41 function MyClass.doMore(self)
Control Structures
10 -- Iterate over all entries in a table
11 for key, value in pairs(table) do
12 doSomething(key, value)
15 -- Iterate over the consecutive numeric entries in a table
16 for i, value in ipairs(table) do