Lua for the impatient
A cheat sheet to get started quickly in Lua
Comments begin with --
Can have multiple lvalues and rvalues in assignment, e.g.
a, b = 5, 10 -- assigns 5 and 10 to a and b respectively
Values have data types, but variables don't. Variables have types global, local, and table fields
Zero and the empty string are truthy, not falsy
By default, all variables will point to nil
Has a type()
function that returns a string naming the type
lvalues and rvalues are just like in C
Operators #
arithmetic operators are just like in C
~=
means not-equal, other relational operators are just like C
logical operators are named not
, and
, and or
Other operators #
..
concatenates two strings
#
returns the length of a string
Loops #
Loops include while
, for
, repeat…until
while #
while(condition)
do
statement(s) -- executes as long as condition is true
end
for #
for i = 10,1,-1
do
print(i) -- counts from 10 down to 1
end
There is a break statement like in C
Conditionals #
if #
if( a < 20 )
then
print("a is less than 20" );
end
if…else #
if( a < 20 )
then
print("a is less than 20" );
else
print("a is not less than 20");
end
Functions #
Mostly like C. A function can be declared local
, otherwise it will default to global
Formal parameters are named without types
Returning multiple values is possible, by using a comma separated list after a return statement
function max(num1, num2)
if (num1 > num2) then
result = num1;
else
result = num2;
end
return result;
end
Because functions are types, they can be passed as parameters
myprint = function(param)
print("This is my print function - ##",param,"##")
end
function add(num1,num2,functionPrint)
result = num1 + num2
functionPrint(result)
end
add(2,5,myprint)
Varargs #
Varargs can be done like this:
function average(...)
result = 0
local arg = {...}
for i,v in ipairs(arg) do
result = result + v
end
return result/#arg
end
print("The average is",average(10,5,3,4,5,6))
Strings #
Strings can be quoted by 'single quotes'
, "double quotes"
or [[pairs of double square brackets]]
The usual escape sequences work, as well as \[
and \]
Manipulation #
string.upper(arg) | To upper case |
string.lower(arg) | To lower case |
string.gsub(main,find,replace) | returns a new string, replacing find with replace |
string.find(main,find,start,end) | returns the start and end index of find. start and end args are optional |
string.reverse(arg) | gra |
string.format(…) | |
string.char(arg) | It's ASCII 😒 |
string.byte(arg, index) | returns the character code for the first, or optionally index'th char |
string.len(arg) | another way to get the length |
string.rep(string,n) | repeats string n times |
Arrays #
Indexing starts at 1 when you assign like this 🙃…
array = {"Apple", "Orange"}
print(array(0))
print(array(1))
prints
nil
Apple
… but you can assign to zero or even negative numbered elements
array[-5] = "Pear" -- valid
Multidimensional arrays are achived either by using
- arrays of arrays
- single dimensional array with index arithmetic
Iterators #
Generic for
iterator #
array = {"Apple", "Orange"}
for key,value in ipairs(array)
do
print(key, value)
end