local name = "Lua" name = "Lua" -- avoid globals local x = nil local flag = true local n = 42
local f = 3.14 local s = "Hello"
local s2 = 'World' local arr = {1, 2, 3} -- 1-indexed! local t = {name = "Lua", version = 5.4} type(x) -- "nil", "number", "string", etc. "Hello " .. "World" #str [[Multi
line
string]] string.format("Hello, %s!", name) string.upper(s)
string.lower(s) string.sub(s, 1, 5) string.find(s, "pattern") string.gsub(s, "old", "new") local t = {}
local arr = {1, 2, 3}
local dict = {a = 1, b = 2} t[1] -- array (1-indexed)
t.key or t["key"] -- dict t[1] = "first"
t.key = "value" #t -- only for array part table.insert(t, value)
table.insert(t, 1, value) -- at index table.remove(t) -- last
table.remove(t, 1) -- at index table.sort(t)
table.sort(t, function(a, b) return a > b end) table.concat(t, ", ") for i, v in ipairs(arr) do
print(i, v)
end for k, v in pairs(t) do
print(k, v)
end for i = 1, #t do
print(t[i])
end if x > 0 then
print("positive")
elseif x < 0 then
print("negative")
else
print("zero")
end -- Only nil and false are falsy
-- 0 and "" are truthy! local x = a or default
local y = condition and a or b while condition do
-- code
end repeat
-- code
until condition for i = 1, 10 do
print(i)
end
for i = 10, 1, -1 do -- step
print(i)
end while true do
if condition then break end
end local function add(a, b)
return a + b
end local add = function(a, b)
return a + b
end local function minmax(t)
return math.min(unpack(t)), math.max(unpack(t))
end
local min, max = minmax({1, 2, 3}) local function sum(...)
local args = {...}
local total = 0
for _, v in ipairs(args) do
total = total + v
end
return total
end local function greet(name)
name = name or "World"
print("Hello, " .. name)
end local function counter()
local count = 0
return function()
count = count + 1
return count
end
end
local c = counter()
c() -- 1
c() -- 2 local Person = {}
Person.name = "John"
function Person:greet()
print("Hello, " .. self.name)
end function Person:new(name)
local obj = {name = name}
setmetatable(obj, {__index = self})
return obj
end
local p = Person:new("Alice") p:greet() -- self is p
p.greet(p) -- equivalent local Student = {}
setmetatable(Student, {__index = Person})
function Student:new(name, grade)
local obj = Person:new(name)
obj.grade = grade
setmetatable(obj, {__index = self})
return obj
end setmetatable(t, mt) getmetatable(t) mt.__index = function(t, k)
return default
end mt.__newindex = function(t, k, v)
rawset(t, k, v)
end mt.__tostring = function(t)
return "MyObject"
end mt.__add = function(a, b)
return a.value + b.value
end -- mymodule.lua
local M = {}
function M.hello()
print("Hello")
end
return M local mymod = require("mymodule")
mymod.hello() package.path = package.path .. ";./lib/?.lua" print("Hello")
io.write("No newline") math.abs(x)
math.floor(x)
math.ceil(x)
math.random()
math.sin(x), math.cos(x) os.time()
os.date()
os.execute("command")
os.getenv("PATH") local f = io.open("file.txt", "r")
local content = f:read("*all")
f:close() local f = io.open("file.txt", "w")
f:write("content")
f:close()