Lua Cheatsheet
Lua Cheatsheet
Lua Cheatsheet
C H E AT S H E E T F O R
Lua
Comments
-- comment
--[[ Multiline
comment ]]
Invoking functions
print()
print("Hi")
-- You can omit parentheses if the argument is one string or table literal
print "Hello World" <--> print("Hello World")
dofile 'a.lua' <--> dofile ('a.lua')
print [[a multi-line <--> print([[a multi-line
message]] message]])
f{x=10, y=20} <--> f({x=10, y=20})
type{} <--> type({})
Tables / arrays
t = {}
t = { a = 1, b = 2 }
t.a = function() ... end
t = { ["hello"] = 200 }
t.hello
Loops
while condition do
end
for i = 1,5 do
end
https://devhints.io/lua 1/7
5/11/2019 Lua cheatsheet
for i = start,finish,delta do
end
repeat
until condition
-- Breaking out:
while x do
if condition then break end
end
Conditionals
if condition then
print("yes")
elsif condition then
print("maybe")
else
print("no")
end
Variables
local x = 2
two, four = 2, 4
Functions
function myFunction()
return 1
end
function myFunctionWithArgs(a, b)
-- ...
end
myFunction()
anonymousFunctions(function()
-- ...
end)
https://devhints.io/lua 2/7
5/11/2019 Lua cheatsheet
-- Splats
function doAction(action, ...)
print("Doing '"..action.."' to", ...)
--> print("Doing 'write' to", "Shirley", "Abed")
end
Lookups
-- The same:
mytable.x
mytable['x']
mytable.y(mytable, a, b)
mytable:y(a, b)
Metatables
mt = {}
print(myobject)
Classes
Account = {}
function Account:new(balance)
https://devhints.io/lua 3/7
5/11/2019 Lua cheatsheet
function Account:withdraw(amount)
print("Withdrawing "..amount.."...")
self.balance = self.balance - amount
self:report()
end
function Account:report()
print("Your current balance is: "..self.balance)
end
a = Account:new(9000)
a:withdraw(200) -- method call
Constants
nil
false
true
-- Relational (binary)
-- __eq __lt __gt __le __ge
== < > <= >=
~= -- Not equal, just like !=
-- Arithmetic (binary)
-- __add __sub __muv __div __mod __pow
+ - * / % ^
-- Arithmetic (unary)
-- __unm (unary minus)
-
-- Logic (and/or)
nil and 10 --> 10
false and nil --> false
10 and 20 --> 20
-- Length
-- __len(array)
#array
-- Indexing
-- __index(table, key)
https://devhints.io/lua 4/7
5/11/2019 Lua cheatsheet
t[key]
t.key
-- String concat
-- __concat(left, right)
"hello, "..name
-- Call
-- __call(func, ...)
dofile("hello.lua")
loadfile("hello.lua")
_G -- Global context
setfenv(1, {}) -- 1: current function, 2: caller, and so on -- {}: the new _G
tonumber("34")
tonumber("8f", 16)
API: Strings
'string'..'concatenation'
s = "Hello"
s:upper()
s:lower()
s:len() -- Just like #s
s:find()
s:gfind()
s:match()
s:gmatch()
s:sub()
s:gsub()
https://devhints.io/lua 5/7
5/11/2019 Lua cheatsheet
s:rep()
s:char()
s:dump()
s:reverse()
s:byte()
s:format()
API: Tables
math.sqrt(144)
math
API: Misc
io.output(io.open("file.txt", "w"))
io.write(x)
io.close()
Reference
https://devhints.io/lua 6/7
5/11/2019 Lua cheatsheet
http://www.lua.org/pil/13.html http://lua-users.org/wiki/ObjectOrientedProgramming
https://devhints.io/lua 7/7