-- 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
do
-- something
end
if condition then
-- something
elseif condition then
-- something
else
-- something
end
-- test before body, repeat until false
while condition do
-- something
end
-- test after body, repeat until true
repeat
-- something
until condition
-- numeric, step defaults to 1
for i = start,finish,step do
-- something
end
-- generic, use with iterators
for a,b in x do
-- something
end
break
: jump out of while
,repeat
, for
-- normal function
function function_name (a, b, ...)
-- something
-- access ... with arg[]
return a, b
end
-- anonymous function, useful in closures
function ()
return "foo"
end
-- assign function_name to some_table
function some_table.function_name ()
return "foo"
end
-- locally scoped function
local function function_name ()
return "foo"
end
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
: ...