
一、基础数据类型
Lua语言只有6种基础数据类型: nil,boolean,number,string,table,function。
在Lua语言中,可是使用type函数获取一个值或者一个变量所属的类型。
print(type("hello world")) -->output:string
print(type(print)) -->output:function
print(type(true)) -->output:boolean
print(type(360.0)) -->output:number
print(type(nil)) -->output:nil
1.1 nil(空)
在Lua中,空值就是nil。如果定义了一个变量,但是在没有赋值之前,它的默认值就是nil。
将nil赋予给一个全局变量就等同于删除它。
1.2 boolean(布尔值)
布尔类型的值只有true和false;但在Lua中,只有nil和false为假,其他所有值都为真,包括0和空字符串。所以 Lua的判断方式和很多常见的开发语言不一样,所以,为了避免在这种问题上出错,可以显示地写明比较的对象,比如:
local a = 0
if a == false then
print("true")
end
1.3 number(数值)
Lua中的number类型用于表示实数,和C/C++类型很相似。但是LuaJIT支持所谓的”dual-number”(双数)模式,即LuaJIT会根据上下文来用整型来存储整数,而用双精度浮点数来存放浮点数。
另外可以使用数学函数math.floor(向下取整)和math.ceil(向上取整)进行取整操作。
1.4 string(字符串)
在Lua中,字符串是不可变的值,如果你要修改某个字符串,就等于创建了一个新的字符串。这种做法有利有弊:
* 好处是即使同一个字符串出现了很多次,在内存中也只有一份
* 劣势也很明显,如果你想修改、拼接字符串,会额外地创建很多不必要的字符串。
举个栗子
local s = ""
for i = 1, 10 do
s = s .. tostring(i)
end
这里循环了10次,但只有最后一次是我们想要的,而中间新建的9个字符串都是无用的,它们不仅占用了额外的空间,也消耗了不必要的CPU运算。
字符串的表达方式
用户可以使用三种方式来表达一个字符串: 单引号、双引号、以及长括号([[]])
- 单引号 : 使用一对匹配的单引号定义一个字符串,如: ‘hello’
- 双引号: 使用一对双引号定义一个字符串,如: “hello”
- 长口号([[]]): 长括号内的整个词法分析过程不受分行限制,不处理任何转义符,并且忽略掉任何不同级别的长括号。这种方式描述的字符串可以包含任何东西,出本级别的反长括号。
print([[ string has \n and \r]])
string has \n and \r
如果字符串中包含了长括号本身,又该如何处理?答案很简单,就是在长括号中间增加一个或者多个=符号,如:
print([=[ string has a [[]]. ]=])
string has a [[]].
1.5 table(表)
table是Lua中唯一的数据结构,实现了一种抽象的“关联数组”。 “关联数组”是一种具有特殊索引方式的数组, 索引通常是字符串(string)或者number类型,但也可以是除nil以外的任意类型的值。
local corp = {
web = "www.google.com", --索引为字符串,key = "web",
-- value = "www.google.com"
telephone = "12345678", --索引为字符串
staff = {"Jack", "Scott", "Gary"}, --索引为字符串,值也是一个表
100876, --相当于 [1] = 100876,此时索引为数字
-- key = 1, value = 100876
100191, --相当于 [2] = 100191,此时索引为数字
[10] = 360, --直接把数字索引给出
["city"] = "Beijing" --索引为字符串
}
print(corp.web) -->output:www.google.com
print(corp["telephone"]) -->output:12345678
print(corp[2]) -->output:100191
print(corp["city"]) -->output:"Beijing"
print(corp.staff[1]) -->output:Jack
print(corp[10]) -->output:360
在内部实现上,table 通常实现为一个哈希表、一个数组、或者两者的混合。具体的实现为何种形式,动态依赖于具体的 table 的键分布特点。
1.6 function(函数)
在Lua中,函数也是一种数据类型,函数可以存储在变量中,可以通过参数传递给其他函数,还可以作为其他函数的返回值。
local function foo()
print("in the function")
--dosomething()
local x = 10
local y = 20
return x + y
end
local a = foo --把函数赋给变量
print(a())
--output:
in the function
30
有名函数的定义本质上是匿名函数对变量的赋值。为说明这一点,考虑
function foo()
end
等价于
foo = function ()
end
local function foo()
end
等价于
local foo = function ()
end
二、表达式
2.1 算术运算符
| 算术运算符 | 说明 |
| + | 加法 |
| - | 减法 |
| * | 乘法 |
| / | 除法 |
| ^ | 指数 |
| % | 取模 |
2.2 关系运算符
| 关系运算符 | 说明 |
| < | 小于 |
| <= | 小于等于 |
| > | 大于 |
| >= | 大于等于 |
| == | 等于 |
| ~= | 不等于 |
Lua语言中“不等于”运算符的写法为: ~=
在使用“==”做等于判断是,要注意对于table,userdate和函数,lua是做引用比较。即: 只有当两个变量引用同一个的对象时,才认为他们相等。eg:
local a = { x = 1, y = 0}
local b = { x = 1, y = 0}
if a == b then
print("a==b")
else
print("a~=b")
end
---output:
a~=b
由于 Lua 字符串总是会被“内化”,即相同内容的字符串只会被保存一份,因此 Lua 字符串之间的相等性比较可以简化为其内部存储地址的比较。这意味着 Lua 字符串的相等性比较总是为 O(1). 而在其他编程语言中,字符串的相等性比较则通常为 O(n),即需要逐个字节(或按若干个连续字节)进行比较。
2.3 逻辑运算符
| 逻辑运算符 | 说明 |
| and | 逻辑与 |
| or | 逻辑或 |
| not | 逻辑非 |
Lua 中的 and 和 or 是不同于 c 语言的。在 c 语言中,and 和 or 只得到两个值 1 和 0,其中 1 表示真,0 表示假。而 Lua 中 and 的执行过程是这样的:
- a and b 如果 a 为 nil,则返回 a,否则返回 b;
- a or b 如果 a 为 nil,则返回 b,否则返回 a。
local c = nil
local d = 0
local e = 100
print(c and d) -->打印 nil
print(c and e) -->打印 nil
print(d and e) -->打印 100
print(c or d) -->打印 0
print(c or e) -->打印 100
print(not c) -->打印 true
print(not d) -->打印 false
注意:
所有逻辑操作符将 false 和 nil 视作假,其他任何值视作真,对于 and 和 or,“短路求值”,对于 not,永远只返回 true 或者 false。
2.4 字符串连接
在 Lua 中连接两个字符串,可以使用操作符“..”(两个点)。如果其任意一个操作数是数字的话,Lua 会将这个数字转换成字符串。注意,连接操作符只会创建一个新字符串,而不会改变原操作数。也可以使用 string 库函数 string.format 连接字符串。
print("Hello " .. "World") -->打印 Hello World
print(0 .. 1) -->打印 01
str1 = string.format("%s-%s","hello","world")
print(str1) -->打印 hello-world
str2 = string.format("%d-%s-%.2f",123,"world",1.21)
print(str2) -->打印 123-world-1.21
由于 Lua 字符串本质上是只读的,因此字符串连接运算符几乎总会创建一个新的(更大的)字符串。这意味着如果有很多这样的连接操作(比如在循环中使用 .. 来拼接最终结果),则性能损耗会非常大。在这种情况下,推荐使用 table 和 table.concat() 来进行很多字符串的拼接,例如:
local pieces = {}
for i, elem in ipairs(my_list) do
pieces[i] = my_process(elem)
end
local res = table.concat(pieces)
当然,上面的例子还可以使用 LuaJIT 独有的 table.new 来恰当地初始化 pieces 表的空间,以避免该表的动态生长。这个特性我们在后面还会详细讨论。
三、控制结构
流程控制语句对于程序设计来说特别重要,它可以用于设定程序的逻辑结构。一般需要与条件判断语句结合使用。Lua 语言提供的控制结构有 if,while,repeat,for,并提供 break 关键字来满足更丰富的需求。
3.1 if/else
if-else是我们熟知的一种控制结构,Lua语言与 C 语言的不同之处是 else 与 if 是连在一起的,若将 else 与 if 写成 “else if” 则相当于在 else 里嵌套另一个 if 语句。
3.1.1 单个 if 分支 型
x = 10
if x > 0 then
print("x is a positive number")
end
运行输出:x is a positive number
3.1.2 两个 if -else分支 型
x = 10
if x > 0 then
print("x is a positive number")
else
print("x is a non-positive number")
end
运行输出:x is a positive number
3.1.3 多个 if -elseif-else分支 型
score = 90
if score == 100 then
print("Very good!Your score is 100")
elseif score >= 60 then
print("Congratulations, you have passed it,your score greater or equal to 60")
--此处可以添加多个elseif
else
print("Sorry, you do not pass the exam! ")
end
运行输出:Congratulations, you have passed it,your score greater or equal to 60
3.1.4 if嵌套
score = 0
if score == 100 then
print("Very good!Your score is 100")
elseif score >= 60 then
print("Congratulations, you have passed it,your score greater or equal to 60")
else
if score > 0 then
print("Your score is better than 0")
else
print("My God, your score turned out to be 0")
end --与上一示例代码不同的是,此处要添加一个end
end
运行输出:My God, your score turned out to be 0
3.2 while
Lua语言中while和其他语言一样,只是没有提供do-while型的控制结构,但是提供了功能相当的repeat。
while 型控制结构语法如下,当表达式值为假(即 false 或 nil)时结束循环。也可以使用 break 语言提前跳出循环。
while 表达式 do
--body
end
x = 1
sum = 0
while x <= 5 do
sum = sum + x
x = x + 1
end
print(sum) -->output 15
注意:
Lua 并没有像许多其他语言那样提供类似 continue 这样的控制语句用来立即进入下一个循环迭代(如果有的话)。因此,我们需要仔细地安排循环体里的分支,以避免这样的需求。
3.3 repeat
Lua 中的 repeat 控制结构类似于其他语言(如:C++ 语言)中的 do-while,但是控制方式是刚好相反的。简单点说,执行 repeat 循环体后,直到 until 的条件为真时才结束,而其他语言(如:C++ 语言)的 do-while 则是当条件为假时就结束循环。
x = 10
repeat
print(x)
until false
该代码将导致死循环,因为until的条件一直为假,循环不会结束
除此之外,repeat 与其他语言的 do-while 基本是一样的。同样,Lua 中的 repeat 也可以在使用 break 退出。
3.4 for
Lua语言中,for 语句有两种形式:数字 for(numeric for)和范型 for(generic for)。
3.4.1 for 数字型
数字型 for 的语法如下:
for var = begin, finish, step do
--body
end
关于数字 for 需要关注以下几点:
- var 从 begin 变化到 finish,每次变化都以 step 作为步长递增 var
- begin、finish、step 三个表达式只会在循环开始时执行一次
- 第三个表达式 step 是可选的,默认为 1
- 控制变量 var 的作用域仅在 for 循环内,需要在外面控制,则需将值赋给一个新的变量
- 循环过程中不要改变控制变量的值,那样会带来不可预知的影响
for i = 1, 5 do
print(i)
end
-- output:
1
2
3
4
5
for i = 1, 10, 2 do
print(i)
end
-- output:
1
3
5
7
9
如果不想给循环设置上限的话,可以使用常量 math.huge:
for i = 1, math.huge do
if (0.3*i^3 - 20*i^2 - 500 >=0) then
print(i)
break
end
end
3.4.2 for 泛型
泛型 for 循环通过一个迭代器(iterator)函数来遍历所有值。
Lua 的基础库提供了 ipairs,这是一个用于遍历数组的迭代器函数。在每次循环中,i 会被赋予一个索引值,同时 v 被赋予一个对应于该索引的数组元素值。
-- 打印数组a的所有值
local a = {"a", "b", "c", "d"}
for i, v in ipairs(a) do
print("index:", i, " value:", v)
end
-- output:
index: 1 value: a
index: 2 value: b
index: 3 value: c
index: 4 value: d
从外观上看泛型 for 比较简单,但其实它是非常强大的。通过不同的迭代器,几乎可以遍历所有的东西, 而且写出的代码极具可读性。标准库提供了几种迭代器,包括用于迭代文件中每行的(io.lines)、 迭代 table 元素的(pairs)、迭代数组元素的(ipairs)、迭代字符串中单词的(string.gmatch)等。
泛型 for 循环与数字型 for 循环有两个相同点:
1. 循环变量是循环体的局部变量;
2. 决不应该对循环变量作任何赋值。
3.5 break, return和goto
3.5.1 break
语句 break 用来终止 while、repeat 和 for 三种循环的执行,并跳出当前循环体, 继续执行当前循环之后的语句。下面举一个 while 循环中的 break 的例子来说明:
-- 计算最小的x,使从1到x的所有数相加和大于100
sum = 0
i = 1
while true do
sum = sum + i
if sum > 100 then
break
end
i = i + 1
end
print("The result is " .. i) -->output:The result is 14
在实际应用中,break 经常用于嵌套循环中。
3.5.2 return
return 主要用于从函数中返回结果,或者用于简单的结束一个函数的执行。return 只能写在语句块的最后,一旦执行了 return 语句,该语句之后的所有语句都不会再执行。若要写在函数中间,则只能写在一个显式的语句块内。
local function add(x, y)
return x + y
--print("add: I will return the result " .. (x + y))
--因为前面有个return,若不注释该语句,则会报错
end
local function is_positive(x)
if x > 0 then
return x .. " is positive"
else
return x .. " is non-positive"
end
--由于return只出现在前面显式的语句块,所以此语句不注释也不会报错
--,但是不会被执行,此处不会产生输出
print("function end!")
end
local sum = add(10, 20)
print("The sum is " .. sum) -->output:The sum is 30
local answer = is_positive(-10)
print(answer) -->output:-10 is non-positive
有时候,为了调试方便,我们可以想在某个函数的中间提前 return,以进行控制流的短路。此时我们可以将 return 放在一个 do … end 代码块中,eg:
local function foo()
print("before")
do return end
print("after") -- 这一行语句永远不会执行到
end
3.5.3 goto
有了 goto,我们可以实现 continue 的功能
for i=1, 3 do
if i <= 2 then
print(i, "yes continue")
goto continue
end
print(i, " no continue")
::continue::
print([[i'm end]])
end
输出结果:
$ luajit test.lua
1 yes continue
i'm end
2 yes continue
i'm end
3 no continue
i'm end
goto 的另外一项用途,就是简化错误处理的流程。有些时候你会发现,直接 goto 到函数末尾统一的错误处理过程,是更为清晰的写法。
local function process(input)
print("the input is", input)
if input < 2 then
goto failed
end
-- 更多处理流程和 goto err
print("processing...")
do return end
::failed::
print("handle error with input", input)
end
process(1)
process(3)