跳到主要内容

Lua 备忘条

学习 Lua 注意避开的坑

  • 数组下标是从 1 开始的(区别于 C、JavaScript 等语言的从 0 开始)
  • string.len 不是取字符串的字符个数,而是取字节数
  • 所有未初始化的变量都是 nil,对一个表中的值赋 nil 会从表中删除它
  • 只有 nilfalse 是逻辑假,其它值都是逻辑真,包括 0
  • 字符串和数字在做数学运算和对比大小时会自动转换,例如 a = '1' + 2
  • 两个浮点数(带小数点的数)不能用全等号 == 做对比,错误用法比如 if 89.7 == (3 * 29.9) then
  • 一个可以完全表示为整数的浮点数和对应的整数相等,例如 1.0 == 1

开发常见运行期错误参考

错误描述片段原因处理方式
attempt to perform arithmetic on a尝试对非数值进行了数学运算(+-*/数学运算之前,检查运算输入是否都为数字
attempt to compare尝试对非法值进行了比较运算(><>=<=比较运算之前,检查运算输入是否双方可以进行比较运算
attempt to concatenate a尝试对非字符串值进行了连接(..在进行字符串连接之前,先确定连接双方都为字符串
attempt to call a尝试调用了一个不是函数的变量调用一个函数之前,先确定其是否为一个函数
attempt to index a尝试对一个非表变量进行索引(下标运算)在从数组变量或关联数组变量中取值前,先确定其是否为一个表
attempt to yield across a C-call boundary尝试在不能让出的调用块中让出require 一个模块的时候,请确认被 require 的模块返回之前没有调用会让出的函数(手册上函数前带叹号)。还有就是不要尝试在带 C 回调的函数中使用会让出的函数(手册上函数前带叹号)。
invalid order function for sorting非法的排序函数,通常发生在排序函数的规则逻辑不够明确的情况下调用排序函数时,明确排序规则,不要出现 a 大于 b 成立同时小于 b 也成立的规则
bad argument #1 to 'xxx' (number expected, got nil)调用某函数时,第 1 个参数的类型不正确,需要 number 却传入了 nil参数错误,传入合适的参数就不会出错了
bad argument #2 to 'xxx' (number has no integer representation)调用某函数时,第 2 个参数无法转换成整数参数错误,传入合适的参数就不会出错了
bad argument #3 to 'xxx'调用某函数时,第 3 个参数非法参数错误,传入合适的参数就不会出错了

os.date 的用法

格式示例描述结果
%Y-%m-%d %H:%M:%Sos.date("%Y-%m-%d %H:%M:%S", 1487356783)一种常用日期时间格式2017-02-18 02:39:43
%Y-%m-%dos.date("%Y-%m-%d", 1487356783)一种常用日期格式2017-02-18
%aos.date("%a", 1487356783)短星期名Sat
%Aos.date("%A", 1487356783)全星期名Saturday
%bos.date("%b", 1487356783)简写的月份名Feb
%Bos.date("%B", 1487356783)月份的全称February
%cos.date("%c", 1487356783)标准的日期的时间串Sat Feb 18 02:39:43 2017
%dos.date("%d", 1487356783)月当中的某一天,取值范围 01 - 3118
%Hos.date("%H", 1487356783)24 小时制的时,取值范围 00 - 2302
%Ios.date("%I", 1487356783)12 小时制的时,取值范围 01 - 1202
%jos.date("%j", 1487356783)年当中的某一天,取值范围 001 - 366049
%Mos.date("%M", 1487356783)分钟,取值范围 00 - 5939
%mos.date("%m", 1487356783)月份,取值范围 01 - 1202
%pos.date("%p", 1487356783)上午为 AM,下午为 PMAM
%Sos.date("%S", 1487356783)秒钟,可能出现闰秒,取值范围 00 - 6143
%wos.date("%w", 1487356783)星期几(星期日为0),取值范围 0 - 66
%xos.date("%x", 1487356783)标准的日期串02/18/17
%Xos.date("%X", 1487356783)标准的时间串02:39:43
%yos.date("%y", 1487356783)不带世纪的年份17
%Yos.date("%Y", 1487356783)带世纪部分的年份2017
%%os.date("%%", 1487356783)百分号%

string 库的用法

基本函数

函数描述示例结果
string.len计算字符串长度string.len("abcd")4
string.rep返回字符串 s 的 n 个拷贝string.rep("abcd", 2)abcdabcd
string.lower返回字符串全部字母小写string.lower("AbcD")abcd
string.upper返回字符串全部字母大写string.upper("AbcD")ABCD
string.format格式化字符串string.format("the value is: %d", 4)the value is: 4
string.sub从字符串里截取字符串string.sub("abcd", 2)bcd
string.sub("abcd", -2)cd
string.sub("abcd", 2, -2)bc
string.sub("abcd", 2, 3)bc
string.find在字符串中查找(显示位置)string.find("cdcdcdcd", "ab")nil
string.find("cdcdcdcd", "cd")1 2
string.find("cdcdcdcd", "cd", 7)7 8
string.match在字符串中查找(显示内容)string.match("cdcdcdcd", "ab")nil
string.match("cdcdcdcd", "cd")cd
string.gsub在字符串中替换string.gsub("abcdabcd", "a", "z")zbcdzbcd 2
string.gsub("aaaa", "a", "z", 3)zzza 3
string.byte返回字符的整数形式string.byte("ABCD", 4)68
string.char将整型数字转成字符并连接string.char(97, 98, 99, 100)abcd

基本模式串

字符类描述示例结果
.任意字符string.find("", ".")nil
%s空白符string.find("ab cd", "%s%s")3 4
%S非空白符string.find("ab cd", "%S%S")1 2
%p标点字符string.find("ab,.cd", "%p%p")3 4
%P非标点字符string.find("ab,.cd", "%P%P")1 2
%c控制字符string.find("abcd\t\n", "%c%c")5 6
%C非控制字符string.find("\t\nabcd", "%C%C")3 4
%d数字string.find("abcd12", "%d%d")5 6
%D非数字string.find("12abcd", "%D%D")3 4
%x十六进制数字string.find("efgh", "%x%x")1 2
%X非十六进制数字string.find("efgh", "%X%X")3 4
%a字母string.find("AB12", "%a%a")1 2
%A非字母string.find("AB12", "%A%A")3 4
%l小写字母string.find("ABab", "%l%l")3 4
%L大写字母string.find("ABab", "%L%L")1 2
%u大写字母string.find("ABab", "%u%u")1 2
%U非大写字母string.find("ABab", "%U%U")3 4
%w字母和数字string.find("a1()", "%w%w")1 2
%W非字母非数字string.find("a1()", "%W%W")3 4

转义字符 %

字符类描述示例结果
%转义字符string.find("abc%..", "%%")4 4
string.find("abc..d", "%.%.")4 5

[] 表示字符集,- 表示连字符,^ 表示字符集的补集

字符类描述示例结果
[01]匹配二进制数string.find("32123", "[01]")3 3
[AB][CD]匹配 ACADBCBDstring.find("ABCDEF", "[AB][CD]")2 3
[[]]匹配一对方括号 []string.find("ABC[]D", "[[]]")4 5
[1-3]匹配数字 123string.find("312", "[1-3][1-3][1-3]")1 3
[b-d]匹配字母 bcdstring.find("dbc", "[b-d][b-d][b-d]")1 3
[^%s]匹配任意非空字符string.find(" a ", "[^%s]")3 3
[^%d]匹配任意非数字字符string.find("123a", "[^%d]")4 4
[^%a]匹配任意非字母字符string.find("abc1", "[^%a]")4 4

() 进行捕获

字符类描述示例结果
()捕获字符串string.find("12ab", "(%a%a)")3 4 ab
string.find("ab12", "(%d%d)")3 4 12

模式修饰符

修饰符描述示例结果
+表示 1 个或多个,匹配最多个string.find("aaabbb", "(a+b)")1 4 aaab
string.find("cccbbb", "(a+b)")nil
-表示 0 个或多个,匹配最少个string.find("zzxyyy", "(xy-)")3 3 x
string.find("zzzyyy", "(x-y)")4 4 y
*表示 0 个或多个,匹配最多个string.find("mmmnnn", "(m*n)")1 4 mmmb
string.find("lllnnn", "(m*n)")4 4 n
?表示 0 个或 1 个string.find("aaabbb", "(a?b)")3 4 ab
string.find("cccbbb", "(a?b)")4 4 b

string.match 的常见用法

描述示例结果
匹配中文string.match("男女abc123", "([^%w%p]+)")男女
匹配英文string.match("男女abc123", "(%a+)")abc
匹配数字string.match("男女abc123", "(%d+)")123
匹配英文和数字string.match("男女abc123", "(%w+)")abc123

math 库的用法

函数名描述示例结果
math.pi圆周率math.pi3.1415926535898
math.abs取绝对值math.abs(-2012)2012
math.ceil向上取整math.ceil(9.1)10
math.floor向下取整math.floor(9.9)9
math.max取参数最大值math.max(2, 4, 6, 8)8
math.min取参数最小值math.min(2, 4, 6, 8)2
math.sqrt开平方math.sqrt(65536)256.0
math.modf取整数和小数部分math.modf(20.12)20 0.12
math.randomseed设随机数种子math.randomseed(os.time())
math.random取随机数math.random(5, 90)
math.rad角度转弧度math.rad(180)3.1415926535898
math.deg弧度转角度math.deg(math.pi)180.0
math.expe 的 x 次方math.exp(4)54.598150033144
math.log计算 x 的自然对数math.log(54.598150033144)4.0
math.sin正弦math.sin(math.rad(30))0.5
math.cos余弦math.cos(math.rad(60))0.5
math.tan正切math.tan(math.rad(45))1.0
math.asin反正弦math.deg(math.asin(0.5))30.0
math.acos反余弦math.deg(math.acos(0.5))60.0
math.atan反正切math.deg(math.atan(1))45.0