-- a comment
--[[ a comment ]]--
g = "foo"
: global variablelocal l = "bar"
: local (block scoped) variableunusual ones
^
exponent~=
: not..
: concatprint()
: print to stdoutpairs()
: iterate over tableipairs()
: iterate over table, stop at first breaka and b or c
== a ? b : c
x = x or v
== if not x then x = v end
not really control flow, just a new block
1do
2 -- something
3end
1if condition then
2 -- something
3elseif condition then
4 -- something
5else
6 -- something
7end
1-- test before body, repeat until false
2while condition do
3 -- something
4end
5
6-- test after body, repeat until true
7repeat
8 -- something
9until condition
1-- numeric, step defaults to 1
2for i = start,finish,step do
3 -- something
4end
5
6-- generic, use with iterators
7for a,b in x do
8 -- something
9end
break
: jump out of while
,repeat
, for
1-- normal function
2function function_name (a, b, ...)
3 -- something
4 -- access ... with arg[]
5 return a, b
6end
7
8-- anonymous function, useful in closures
9function ()
10 return "foo"
11end
12
13-- assign function_name to some_table
14function some_table.function_name ()
15 return "foo"
16end
17
18-- locally scoped function
19local function function_name ()
20 return "foo"
21end
t = {}
: empty tablet = { "foo"="bar" }
: t["foo"] = "bar"
t = { "foo", "bar" }
: t[1] = "foo"
, t[2] = "bar"
error("some error")
: raise an errorassert(condition, "some error")
: assert, raise an errorpcall(some_function)
: catch an error, returns true
+ values or false
+ errorref lua 5.4 index
arg
: table of args, for scripts, functions with variable args_G
: table holding globalscoroutine
: coroutine functions, create, yield, resume, ...table
: table manipulation: insert, remove, sort, concat, ...string
: string manipulation: len, lower, upper, char, byte, sub, rep, reverse, format, find, gsub, gfind, ...io
: io functions: lines, read, write, input, output, open, flush, close, stdin, stdout, stderr, ...open
: return a file handle with functions: lines, read, write, seek, flush close, ...os
: date, time, difftime, getenv, execute, exit, ...math
: ...utf8
: ...debug
: ...