Lua Small scripting language
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
lua <file.lua>
lualua -e "<code>"
local x = 10for i = 1, 10 do ... end
for k, v in pairs(<t>) do
string.format("%d", n)
pcall(<f>, ...)
Concepts
- Table your data
- Metatable __index, __add...
- Metamethod function or table
- Value returned
Run the interpreter
lua <file.lua> [<args>]
lualua -e "<code>"
lua -i <file.lua>
arg[0]Compile with luac
luac -o <out.luac> <file.lua>
luac -p <file.lua>
Types and variables
local <name> = <value>
type(<v>)<a> and <b> or <c>
tostring(<v>) / tonumber(<s> [,
<base>])
#<t_or_s><a> .. <b>
Operators
<a> // <b>
<a> % <b>
<a> ^ <b>
Control flow
if <c> then ... elseif <c> then ... else ...
end
for i = <a>, <b> [, <step>] do ...
end
for i, v in ipairs(<t>) do ... end
for k, v in pairs(<t>) do ... end
while <c> do ... end
goto <label> ... ::<label>::
Functions
function <name>(<args>) ... end
local function <name>(<args>) ... end
function <name>(...) local a = {...} end
select("#", ...)
select(<n>, ...)
Tables and metatables
local t = {1, 2, 3}
local t = {name = "x", ["k k"] = 1}
<t>.<key> <t>[<expr>]
setmetatable(<t>, <mt>)
mt.__index = <table_or_fn>
mt.__call = <fn>
mt.__tostring = <fn>
rawget(<t>, <k>) / rawset(<t>, <k>,
<v>)
obj:method(<args>)
function Class:method() ... end
Strings
#<s><s>:sub(<i> [, <j>])
<s>:upper() <s>:lower()
<s>:rep(<n> [, <sep>])
<s>:find(<pat> [, <init> [, true]])
<s>:match(<pat>)
<s>:gmatch(<pat>)
<s>:gsub(<pat>, <repl> [,
<n>])
string.format(<fmt>, ...)
utf8.char(<cp>) utf8.len(<s>)
Patterns
. %a %d %l %u %s %w %p %x
%A %D %S %W[abc] [a-z] [^0-9]
* + - ?^ $( )%b()%f[%w]%. %% %-"^%s*(.-)%s*$"
Format specifiers
%d %i%5.2f%s%x %X %oTable library
table.insert(<t>, <v>)
table.insert(<t>, <pos>, <v>)
table.remove(<t> [, <pos>])
table.concat(<t> [, <sep>])
table.sort(<t> [, <cmp>])
table.unpack(<t> [, <i> [, <j>]])
table.pack(<values>)
Math
math.floor(<x>) math.ceil(<x>)
math.max(<a>, ...) math.min(<a>, ...)
math.abs(<x>) math.sqrt(<x>)
math.random(<m>, <n>)
math.random()math.tointeger(<x>)
math.huge math.pi
Errors
error(<msg> [, <level>])
assert(<v> [, <msg>])
pcall(<f>, ...)
xpcall(<f>, <handler>, ...)
xpcall(<f>, debug.traceback)
Modules
local m = require("<name>")
return Mpackage.pathpackage.loaded[<name>] = nil
dofile(<file>)
Coroutines
coroutine.create(<f>)
coroutine.resume(<co>, ...)
coroutine.yield(...)
coroutine.wrap(<f>)
coroutine.status(<co>)
OS and I/O
print(<values>)
io.write(<values>)
io.read("l")
io.open(<path>, "r")
f:read("a")
f:write(<values>)
f:lines()f:close()io.lines(<path>)
io.stderr:write(<msg>)
os.getenv(<name>)
os.time()os.date("%Y-%m-%d %H:%M" [, <t>])
os.execute(<cmd>)
os.exit([<code>])
LuaRocks
luarocks install <rock>
luarocks install --local <rock>
luarocks listluarocks search <text>
luarocks remove <rock>
luarocks pathRecipes
local Animal = {}Animal.__index = Animalfunction Animal.new(name) return setmetatable({name = name}, Animal) endfunction Animal:speak() return self.name .. " ..." endlocal Dog = setmetatable({}, {__index = Animal})Dog.__index = Dogfunction Dog.new(name) return setmetatable(Animal.new(name), Dog) end
local parts = {}for w in (s .. ","):gmatch("(.-),") do parts[#parts + 1] = w end
local f, err = io.open(path, "r")if not f then error(err) endlocal text = f:read("a")f:close()
local gen = coroutine.wrap(function() for i = 1, 3 do coroutine.yield(i) endend)print(gen(), gen(), gen())



