Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

LUA quick start-up guide
  • Lua is a simple, general-purpose scripting language 

  • Lua provides the performance and security, 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 is LUA?
Variables and Simple Data Types
Lua has variables and simple types include nil, string,
number (integer, floating point), and Boolean values


local name = "Edgar"
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
local s = "Text " .. "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 = 3
x, y = 3, 4

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

Constructors can also create members
local T = {}
local T['x'] = 4

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

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

Tables can act like records. The following are equivalent
local T = { ["+"] = "plus", ["-"] = "minus" }
local T = { "first", "second" }
local T = { [1] = "first", [2] = "second" }

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

Functions and Function Calls
Lua has functions and function calls
Unlike many languages, Lua
supports multiple return values

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

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

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

if a < 0 then
    a = -a
end
 

if a < b then
    c = "less"
elseif a == b then
    c = "equal"
else
    c = "greater"
end
 

 


while a > 10 do
    f(a)
    a = a - 1
end 
repeat
    a = f(a)
until a == 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

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)

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

for i = 1, 9, 2 do
    f(i)
end

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

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

Use a spacebar or arrow keys to navigate