Skip to content

Instantly share code, notes, and snippets.

@samuelfvlcastro
Last active March 14, 2023 07:27
Show Gist options
  • Save samuelfvlcastro/32af4bfdafe9afb7c4a55b66d2d6550a to your computer and use it in GitHub Desktop.
Save samuelfvlcastro/32af4bfdafe9afb7c4a55b66d2d6550a to your computer and use it in GitHub Desktop.
Lua cheat sheet
-- Variables
v1, v2, v3 = 3, 2, 1
print(v1,v2,v3, v4 --[[by default it's nil]])
v5, v6, v7 = true, 1, 0 -- Are all "true" values
v8, v9 = false, nil -- Are the only "false" values
--[[ Operators
1 - The logical AND, OR, and NOT
2 - The inequality operator is ~= instead of !=.
3 - .. is the operator for string concatenation.
4 - ^ raises a number to a power
5 - # is an operator used to get the length of tables and strings
]]
-- Conditionals
if state == 5 then
print(state)
end
-- Loops
while x < 10 do
x = x + 2
print(x)
end
while true do
print("run once")
break
end
i = 5
repeat
i = i - 1
print(i)
until i == 1
for i = 1, 11, 2 do -- for var = start, limit, step do
x = x * i
print(x)
end
if pcall(function () print(a[i]) end) then -- protected function call
print("cool")
else
print("error")
end
error("string expected") -- throw error
-- Generic for loops
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
b = { 20, 30, 40 }
for key, value in pais(days) do
print(key, value)
end
for index, value in ipairs(b) do
print(index, value)
end
-- Functions
function first(v1, v2)
local x, y = second(v1 or 1, v2 or 2)
print(x, y)
end
function second(v1, v2)
return v1+1, v2+1
end
first(2, 3)
first() -- the "or" works as a optional value alternative
function sum(...) -- variable arguments
local ret = 0
for i, v in ipairs{...} do
ret = ret + v
end
return ret
end
sum(2, 3, 4, 5) -- 14
t = {}
function t:func(x, y) -- self syntatic sugar
self.x = x
self.y = y
end
t:func(1, 1)
print(t.x) -- 1
-- Arrays
arr = {"sam", "cas", "tro"}
print(arr[1], arr[2], arr[3]) -- indexes start at 1 !!!
print("The array size is: " .. #arr)
-- Tables
x = 5
k = "anokey"
ta = {akey = 2, [k] = 3}
ta[k] = 4
print(ta["akey"], ta[k])
ta.akey = 4 -- syntactic sugar for string keys
print(ta.akey, ta.anokey)
-- Table module
t = {34, 54, 65, 34, 464, 64}
table.insert(t, 50) -- add 50 at the table end
table.insert(t, 3, 89) -- add 89 at the index 3
table.remove(t, 2) -- remove the value at index 2
table.sort(t) -- sorts via less then (<)
-- String
s = "foo\nbar" -- new line
t = 'he said "hello world"'
u = "Hello \"world\"" -- escape double quotes
v = [[
<html>
<body>
<p>Hello world!</p>
</body>
</html>
]] -- multi-line string
-- String module
string.lower("HeLLO") -- hello
string.upper("Hello") -- HELLO
string.reverse("world") -- dlrow
string.char(87) -- W
string.sub("hello world", 2, 5) -- ello
-- Regex strings
string.gsub("hello 42", "(%d+)", "%1 3") -- hello 42 3
string.gsub("heLLo", "(%u)", "") -- heo
string.gsub("2 + 2 = 4", "(%d)", function(s) -- 4 + 4 = 8
return s * 2
end)
for w in string.gmatch("good morning chaps", "%w+") do -- prints each word
print(w)
end
-- Numbers
-- variations: 4, 0.4, .4, 4.3, 0xFF, 0xA33FF0E, 8.7e12, 8.7e+12, 8e-12
-- Loading files / modules
func = loadfile("module.lua") -- loads the file into a function
func() -- run the function
x = dofile("module.lua") -- loads and runs the file and stores the return on x
-- Package system
require("cheese") -- takes a path to a Lua file and searches for it
require("folder.subfolder.file") -- require replaces each dot with the directory separator
-- Executing strings
loadstring("print('stuff')")() -- loads and runs the lua code inside the string
local Person = {}
function Person:new(fname, lname, age)
local meta = setmetatable({},self)
self.__index = self
self.fname = fname
self.lname = lname
self.age = age
function Person:fullName()
return self.fname .. ' ' .. self.lname
end
return meta
end
return Person
--[[
Person = require("main")
sam = Person:new("samuel","castro", 30)
print(sam:fullName())
]]
local pval = "old" -- PASSED BY VAL: nil, boolean, number, string
local pref = {"old1", "old2"} -- PASSED BY REF: function, userdata, thread, table
function valuefunc(a)
a = "new" -- new var NOT the passed
end
function reffunc(b)
-- ref to the passed table
b[1] = "new1"
b[2] = "new2"
end
valuefunc(pval) -- passing a string by value
reffunc(pref) -- passing a table by ref
print(pval, pref[1],pref[2]) -- old new1 new2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment