Introduction to Lua

This crash course provides a basic introduction to the language, enough to begin writing your own basic scripts.

Data types

Lua has 8 basic data types:


Arithmetic operators

Lua has the usual arithmetic operators, +, -, *, /, ^ (exponentiation). The precedence is the usual.


The following are available, >, >=, < <=, ==, ~= (not equal)

Logical operators

The following are available and, or, not. false and nil are considered false, everything else is considered true (so the number 0 is also considered true). The operator and returns the first argument if it is false and the second otherwise. The operator or returns the first argument if it not false otherwise it returns the second argument.



Single line comments begin with --
    --This line is not executed
Multiline comments are written with similar delimitation as strings as shown below:
    --[[This line is not executed
     nor this one   ]]
    --[=[ another long
    -[==[ yet three more
        not executed


There is no need to declare a variable before assigning to it. By default variables are global.
assigns 1 to the variable a.

Lua allows multiple assignments:
    x = 5
    a,b = 5*x,"dog"
assigns 25 to a, and "dog" to b.

To create a local variable, the keyword "local" is used. This variable will be visible only within the block it is declared in. A block is the body of a control structure such as function or a loop.
        local a,b = 1, 5
creates local variables "a" and "b".

Control structures

Lua has the following control structures


Functions perform the bulk of the work in Lua. They are called by using parenthesis after their name:
calls the function "print" with the argument a string." The call
    x = math.sin(2/3)
assigns the sine of 2/3 to the variable "x"."

Creating functions

Functions are created as in the example below
    function f(x)
        local y=2*x
        return y
This creates a function f, which returns the input doubled. A function can return more than one value.
    function g(x)
        local y=2*x
        local z=x^2
        return y,z
This function returns two values, the double of a number and the square. Both values can be recovered by:
    local s,ss=g(5)
The variable s will contain the value 10, and ss will contain the value 25.


THE ONLY data structure in Lua is the table. It is an associative array which can be indexed with numbers, strings or other Lua data types (except nil). Note that by default Lua tables indicies start from 1.

Tables are created in the following ways
    -- create empty table
    -- create integer indexed table
    print(a[1]) -- prints 3.14
    print(a[4]) -- prints 2.71
As above, brackets are used to access table elements. Another method is by using the period notation
A Lua table can contain any valid Lua object, including functions and tables. Standard Lua uses tables of functions in lieu of namespaces.

Objects (the colon notation)

Lua is not an object oriented language, but it does offer basic support via the colon operator. A table can contain both variables and functions. Lua enables functions in a table to refer to the table they live in.

Suppose I have the following table with variables a and b. Also, suppose this table contains the function sum which adds the two numbers and a constant. The code for the situation is the following:
    nn={a=2, b=3}
    function nn.sum(t,c)
        return (t.a+t.b+c)
Note that the sum function has been written as a field of the table nn, so the table nn contains three things. The sum function is defined to take a parameter t so that it can be passed any table that contains a and b.

The naive way to get call the sum function is
The table nn is passed into the function. Lua allows an easier way to make such a call using the colon:
    print( nn:sum(7) )
What this does is secretly pass nn as the first parameter in the call to sum and 7 as the second parameter.

This notation also allows an alternative way to define sum:
    function nn:sum(c)
        return (self.a+self.b+c)
Note that the secretly passed parameter is called self.