var = 50 -- a global variable
print(var) --> 50
do
local var = 100 -- a local variable
print(var) --> 100
end
print(var) --> 50
-- The global var (50) still exists
-- The local var (100) has gone out of scope and can't be accessed any longer.
num = 20 -- a number
num = 20.001 -- still a number
str = "zaldrizes buzdari iksos daor" -- a string
tab = {1, 2, 3} -- a table (these have their own category)
bool = true -- a boolean value
bool = false -- the only other boolean value
print(type(num)) --> 'number'
print(type(str)) --> 'string'
print(type(bool)) --> 'boolean'
type(type(num)) --> 'string'
-- Functions are a type too, and first-class values in Lua.
print(type(print)) --> prints 'function'
old_print = print
print = function (x) old_print "I'm ignoring the param you passed me!" end
old_print(type(print)) --> Still prints 'function' since it's still a function.
-- But we've (unhelpfully) redefined the behavior of print.
print("Hello, world!") --> prints "I'm ignoring the param you passed me!"
nil
Another type in Lua is nil
. The only value in the nil
type is nil
. nil
exists to be different from all other values in Lua. It is a kind of non-value value.
print(foo) -- This prints nil since there's nothing stored in the variable 'foo'.
foo = 20
print(foo) -- Now this prints 20 since we've assigned 'foo' a value of 20.
-- We can also use `nil` to undefine a variable
foo = nil -- Here we set 'foo' to nil so that it can be garbage-collected.
if nil then print "nil" end --> (prints nothing)
-- Only false and nil are considered false; every other value is true.
if 0 then print "0" end --> 0
if "" then print "Empty string!" --> Empty string!
a = 3
b = a + 20 a = 2 print(b, a) -- hard to read, can also be written as
b = a + 20; a = 2; print(a, b) -- easier to read, ; are optional though
true and true --> returns true
true and 20 --> 20
false and 20 --> false
false or 20 --> 20
true or 20 --> true
tab or {}
--> returns tab if it is defined
--> returns {} if tab is undefined
-- This is useful when we don't know if a variable exists
tab = tab or {} -- tab stays unchanged if it exists; tab becomes {} if it was previously nil.
a, b = 20, 30 -- this also works
a, b = b, a -- switches values
function name(parameter)
return parameter
end
print(name(20)) --> 20
-- see function category for more information
name = function(parameter) return parameter end -- Same as above
Only false
and nil
evaluate as false, everything else, including 0
and the empty string evaluate as true.
tab = {"lots", "of", "data"}
tab = nil; collectgarbage()
-- tab does no longer exist, and doesn't take up memory anymore.
tab1 = {"a", "b", "c"}
tab2 = tab1
tab2[1] = "d"
print(tab1[1]) --> 'd' -- table values only store references.
--> assigning tables does not copy its content, only the reference.
tab2 = nil; collectgarbage()
print(tab1) --> (prints table address) -- tab1 still exists; it didn't get garbage-collected.
tab1 = nil; collectgarbage()
-- No more references. Now it should actually be gone from memory.
These are the basics, but there's a section about tables with more information.
if (condition) then
-- do something
elseif (other_condition) then
-- do something else
else
-- do something
end
There are two types of for
loop in Lua: a numeric for
loop and a generic for
loop.
A numeric for
loop has the following form:
for a=1, 10, 2 do -- for a starting at 1, ending at 10, in steps of 2
print(a) --> 1, 3, 5, 7, 9
end
The third expression in a numeric for
loop is the step by which the loop will increment. This makes it easy to do reverse loops:
for a=10, 1, -1 do
print(a) --> 10, 9, 8, 7, 6, etc.
end
If the step expression is left out, Lua assumes a default step of 1.
for a=1, 10 do
print(a) --> 1, 2, 3, 4, 5, etc.
end
Also note that the loop variable is local to the for
loop. It will not exist after the loop is over.
Generic for
loops work through all values that an iterator function returns:
for key, value in pairs({"some", "table"}) do
print(key, value)
--> 1 some
--> 2 table
end
Lua provides several built in iterators (e.g., pairs
, ipairs
), and users can define their own custom iterators as well to use with generic for
loops.
local a = 10
do
print(a) --> 10
local a = 20
print(a) --> 20
end
print(a) --> 10