Lua Tutorial for Programmers

Introduction

Lua means Moon in Portuguese. Lua is fast, embeddable and lightweight scripting language. It was created in 1993 in Brazil.

This short tutorial on Lua is for programmers who have experience with 'C like' programming languages such as C, C++, Objective C and Java.

Types and Values

As in many scripting languages, in Lua variables don't have types. Only values have types. There are 8 value types in Lua: nil, boolean, number, string, table, function, thread and userdata. Out of these, following are used the most:

Though following are available, they are used only in special cases:

Variables

Comments

Single line comment

Multi line comment

The type function

The type function gives the type of the value currently held by a variable.

Strings

String Concatenation

The + Operator and Number Conversion

To convert a number to a string you can either use tostring() function or concatenate the number with empty string.

Operator Precedence

^ represents 'exponentiation' operator. Also note that there are no ++ or -- operators.

Multiple Assignment

Lua allows multiple assignment of values to the variables.

Conditionals and Loops

if...else

while

repeat...until

repeat...until executes the body of the block until the condition becomes true. Also the condition is checked after the body of the block is executed, hence the block is executed at least once.

Numeric for

The numeric for has following syntax:

Here i will be 0 at the start. It will be incremented by 2 (this is called as step) for every loop iteration till it becomes 10.

Please note that the variable i is local to the for block.

If you omit the step, it defaults to 1.

Example:

Another example:

If you run this example you will see that this loop executes 11 times to give output:

This shows that all the three expressions are evaluated at the start of the loop. So at the start of the loop k - 100 = 0 hence the loop executes till i becomes 0.

break

In Lua break can appear only before end or else or until.

Tables

Table is a multipurpose data structure in Lua. It can be used as an array or a hash.

Array

As per Lua convention arrays start at index 1. However this is not mandatory. You can even have negative indexes if you want. However Lua libraries expect arrays to start at index 1.

Another way to create an array is:

This will output:

Hash

You can access the values using either [] or . notation.

Generic for Loop

Generic for loop can be used to traverse tables as keys and values.

To get both keys and values:

Functions

Example:

Please note the use of keyword local. Unless specified local the variables defined in functions are treated as global variables. If we write the above program without the local declaration:

Multiple return values

Lua functions can return multiple return values. It's logical as Lua supports multiple assignment.

Variable number of arguments

Example:

Another Example:

Functions as arguments

Closures

Non global functions

References

About the Author

My name is Pritam. I am cofounder of Mobisoft Infotech. I live in Pune, India.

Copyright

This work is available under the Creative Commons Attribution-ShareAlike License