包含最重要概念、函数、方法等的 Lua 备忘单。 初学者的完整快速参考。
local table = {}
默认的初始索引会从 1 开始
local array = { "apple", "pear", "orange", "grape" }
print(array[1]) -- apple
for k, v in pairs(array) do
print(k .. " : " .. v)
end
-- 1 : apple
-- 2 : pear
-- 3 : orange
-- 4 : grape
local array = {}
array.one = "apple"
array["two"] = "peach"
print(array.one) -- apple
print(array.two) -- peach
a = "hello " .. "world" -- 改变 变量
t.n = t.n + 1 -- 改变 table
-- 给多个变量赋值
-- 情况1:a 未定义过
a, b = 10, 2*a -- 报错:a 是 nil
-- 情况2:a 已定义(比如 a=5)
a = 5
a, b = 10, 2*a -- 右侧的 a=5,结果 a=10, b=10(不会报错)
local x, y = 1, 3
x, y = y, x
print(x, y) -- 3, 1
local tab = {}
tab.one = 2
tab.two = 1
tab["one"], tab["two"] = tab.two, tab.one
print(tab.one, tab.two) -- 1 2
如果变量个数大于值的个数,按变量个数补足 nil
a, b, c = 1, 3
print(a,b,c) --> 1 3 nil
如果变量个数小于值的个数,多余的值会被忽略
a = 1
local a, b = a, a + 1, a + 2
print(a, b) --> 1 2
local function isValidNumber(num)
if type(num) ~= "number" then
goto invalidNumber -- 无条件地跳转到代码中的标签 `::invalidNumber::`
end
print("Valid number:", num)
return true
::invalidNumber::
print("Invalid number:", num)
return false
end
isValidNumber(123) -- 输出: Valid number: 123
isValidNumber("abc") -- 输出: Invalid number: abc
| 符号 | 含义 |
|---|---|
== | 等于 |
~= | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<= | 小于等于 |
local a, b = 4, 3
print(a < b) -- false
print(a <= b) -- false
print(a == b) -- false
print(a ~= b) -- true
print(a > b) -- true
print(a >= b)-- true
| 符号 | 含义 |
|---|---|
and | 逻辑与 |
or | 逻辑或操作符 |
not | 逻辑非操作符 |
local a, b = true, false
print(a and b) -- false
print(a and not b) -- true
print(a or b) -- true
-- 全部转换为大写
string.upper("str") -- STR
-- 全部转换为小写
string.lower("STR") -- str
-- 指定替换的字符串个数, 最后一个参数可选,默认是全部替换
string.gsub("aaaa", "a", "b", 3) -- bbba 3
string.gsub("Today is 29/01/2019", "%d%d/%d%d/%d%d%d%d", "a good day.")
-- Today is a good day.
-- 查找第一个匹配的字符串,第三个参数可以提供开始查找的位置,默认从 1 开始
-- 如果未找到,则返回 nil
string.find("referference", "fer") -- 3 5
string.find("Today is 29/01/2021", "%d%d/%d%d/%d%d%d%d") -- 10 19
-- 字符串反转
string.reverse("fw") -- wf
-- 格式化字符串
string.format("value:%c", 1) -- value:a
-- 转换字符并拼接
string.char(97,98,99,100) -- abcd
-- 将字符转化为整数值。 int 用来指定某个字符,默认第一个字符
string.byte("ABCD",4) -- 68
-- 计算字符串长度
string.len("abc") -- 3
-- 返回字符串的 n 个拷贝
string.rep("fw", n) -- fwfw
-- 剪切字符串,第三个参数可选,默认是字符串长度
string.sub("referference", 5, 6) -- rf
| :- | :- |
|---|---|
%a | 与任何字母配对 |
%c | 与任何控制符配对(例如\n) |
%d | 与任何数字配对 |
%l | 与任何小写字母配对 |
%p | 与任何标点(punctuation)配对 |
%s | 与空白字符配对 |
%u | 与任何大写字母配对 |
%w | 与任何字母/数字配对 |
%x | 与任何十六进制数配对 |
%z | 与任何代表0的字符配对 |
第三个参数可选,默认从 1 开始。如果没有捕获组返回整个字符串,匹配失败返回 nil
string.match(
"I have 2 questions for you.",
"(%d+) (%a+) "
) -- 2 questions
返回一个迭代器函数,每次调用迭代器函数,如果参数 pattern 描述的字符串没有找到,迭代函数返回nil
for world in string.gmatch("I have 2 questions for you.", "%a+") do
print(world)
end
-- I
-- have
-- questions
-- for
-- you
-- 一个比任何数字都大的浮点数
math.huge
-- 最小值的整数
math.mininteger
local a = math.abs(-1) -- 1
-- 返回不小于该数到最小整数
local b = math.ceil(1.2) -- 2
-- 返回不大于该数到最大整数
local c = math.floor(1.2) -- 1
-- 取余
local d = math.fmod(9.9, 9) -- 0.9
-- 返回最大值
local g = math.max(1, 2, 3) -- 3
-- 返回最小值
local h = math.min(1, 2, 3) -- 1
-- 返回参数的平方根
local r = math.sqrt(9) -- 3
-- 返回数字的类型,
local l = math.type(1.2) -- float
local m = math.type(3) -- integer
local n = math.type("") -- nil
-- 返回以指定底底对数
local e = math.log(4, 2) -- 2
-- 返回以 e 为底的自然对数
local f = math.exp(2) -- 7.3890560989307
-- 返回 [0,1) 区间内一致分布的浮点伪随机数
math.random()
-- 返回 [1, n] 区间内一致分布的整数伪随机数
math.random(10)
-- 返回 [n, m] 区间内一致分布的整数伪随机数
math.random(10, 100)
-- 无符号整数比较,参数一 小于 参数二 则返回 true,否则返回 false
local o = math.ult(1, 10)
-- 如果参数可以转换为一个整数,则返回该整数,否则返回 nil
local p = math.tointeger("3") -- 3
local q = math.tointeger(0.32) -- nil
-- 返回整数和小数部分
local i, j = math.modf(3.14) -- 3 0.14
-- 圆周率
math.pi -- 3.1415926535898
-- 正弦方法(以下皆是以弧度表示)
math.sin(math.pi / 2) -- 1.0
-- 余弦方法
math.cos(math.pi) -- -1.0
-- 正切方法
math.tan(math.pi / 4) -- 1.0
-- 反正弦方法(以下皆是以弧度表示)
math.asin(1.0) -- 1.5707963267949
-- 反余弦方法
math.acos(1.0) -- 0.0
-- 反正切方法
math.atan(1.0) -- 0.78539816339745
-- 角度转换为弧度
math.rad(90) -- 1.5707963267949
-- 弧度转换为角度
math.deg(math.pi) -- 180.0
-- 用于连接 table 中指定的元素
-- table.concat(table [, sep [, start [, end]]])
local a = { "apple", "orange", "peach" }
print(table.concat(a, "->", 2, 3)) -- orange->peach
-- 用于向指定闻之插入元素。默认数组末尾
-- table.insert(table, [pos,] value)
local a = { "apple", "orange", "peach" }
table.insert(a, 1, "pear")
print(a[1]) -- pear
-- table.move(a1,f,e,t[,a2])
-- 表a1,a1下标开始位置f,a1下标结束位置e,t选择移动到的开始位置(如果没有a2,默认a1的下标)
local array = { "a", "b", "c" }
for i,v in pairs(table.move(array, 1, 3, 2)) do
print(v)
end -- a a b c
-- table.sort (table [, comp])
-- 排序,默认是升序
local array = { "a", "c", "b" }
local f = function(a, b)
return string.byte(a) - string.byte(b) > 0
end
table.sort(array, f)
for i, v in pairs(array) do
print(v)
end -- c b a