= Lua in Relatively Few Words


This quick tutorial was created by the great people over at https://www.webscript.io/ and is republished here with their permission.

Lua is a simple, general-purpose scripting language. Lua was chosen by Webscript.io and info-beamer because Lua's open-source implementation provides the performance and security that we needed, while being extremely small and efficient. We believe that most people who are new to Lua will pick up the basics in minutes and feel comfortable within hours. What follows is an extremely brief introduction that covers the basics. See http://www.lua.org/docs.html for more thorough Lua reference materials.

Variables and Simple Data Types

Lua has variables and simple types include nil, string, number (integer, floating point), and Boolean values.

local name = "Roberto"
local x = 1
local pi = 3.14159
local flag = false

Literal values

Typically, Lua strings are enclosed in single or double quotes. They can also be enclosed in double brackets (e.g. [[hello world]]), which allows newlines in the string literal.

Lua Boolean literals are true and false.

Strings

Strings are an essential Lua data type. Common string operations follow:

local s = "String " .. "concatenation"
local n = #s  -- the length of s in bytes

Assignment

Lua uses = as the assignment operator. In addition to typical assignment, Lua also supports multiple assignment:

x, y = 1, 2

Operators

Lua uses <, <=, ==. >=, >, ~= as comparison operators on numeric values.

Lua uses and, or, and not as logical operators.

Tables

Lua has only one structured data type, tables, which are associative arrays. The literal constructor {} represents a new empty table.

local T = {}
local T['x'] = 4

Constructors can also create members.

local T = { x = 1, y = 3.14 }

If the member name is not a proper identifier name, then it must be quoted and wrapped in brackets

local T = { ["-"] = "minus", ["+"] = "plus" }

If no member names are given, then the constructor is treated like an array, with the key values 1, 2, 3, …. The following are equivalent

local T = { "first", "second" }
local T = { [1] = "first", [2] = "second" }

Tables can act like records. The following are equivalent:

local T["x"] = 3
local T.x = 3

Functions and Function Calls

Lua has functions and function calls.

function fact(n)
    if n == 1 then
        return 1
    end
    return n*fact(n-1)
end

Unlike many languages, Lua supports multiple return values.

function f(x, y)
    return x+y, x-y
end
 
local a, b = f(4, 2)

Lua function calls may omit the parentheses when there is a single argument that is either a string literal or a table constructor. When you see this:

fn { foo = baz }

It is syntactic sugar for

fn({ foo = baz })

Similarly, foo 'bar' is sugar for foo('bar').

Control Statements

Lua has if-elseif-else-then, while-loop, and repeat-loop statements.

if x < 0 then
    x = -x
end
 
if x < y then
    s = "less"
elseif x == y then
    s = "equal"
else
    s = "greater"
end
 
while x > 10 do
    f(x)
    x = x- 1
end
 
repeat
    x = f(x)
until x == 0

Lua also has a general for-loop with numeric indices. The for-loop includes two or three index values that represent the start, end, and (optional) step values.

for i = 0, 10, 2 do
    f(i)
end

Lua also includes a for-loop that iterates over the values in a list. This is often used to iterate over the key/value pairs in a table using either pairs(T) or ipairs(T).

for k, v in pairs(T) do
    -- iterate over all entries in T
    fn(k, v)
end

for k,v in ipairs(T) do
    -- iterate over all integer-keyed entries in T
    fn(k, v)
end

Lua includes a break-statement that will exit an enclosing loop:

for k, v in pairs(T) do
    if k == 0 then break end
end

Advanced

Lua is a rich, modern programming language, and this introduction is only meant to help you get started. Please refer to the excellent online documents to learn the full language. See http://www.lua.org/docs.html for more.