数据类型
Lua中有8个基本类型:nil
boolean
number
string
table
function
thread
userdata
Boolean
Lua 把 false 和 nil 看作是”假”,其他的都为”真”
Number
Lua 默认只有一种 number 类型 – double
String
字符串由一对双引号或单引号来表示。
也可以用 2 个方括号 [[]]
来表示一块字符串。
1
2
3
4
5
6
7
8
html = [[
<html>
<head>Hello world</head>
<body>
<p>Hello</a>
</body>
</html>
]]
使用 ..
进行字符串连接
1
"a" .. 'b'
使用 # 来计算字符串的长度
1
#"Hello"
支持常见的转义符 \n
\t
Table
不同于其他语言的数组把 0 作为数组的初始索引,在 Lua 里表的默认初始索引一般以 1 开始。
变量
Lua 中的变量默认为全局变量,除非用 local 显式声明为局部变量。
局部变量的作用域为从声明位置开始到所在语句块结束。
变量的默认值均为 nil
。
运算符
算数运算符
+
-
*
/
%
^
关系运算符
==
~=
>
<
>=
<=
逻辑运算符
and
or
not
其他运算符
..
- 字符串连接
#
- 返回字符串或表的长度
循环
while
1
2
3
while (true) do
print('Hello')
end
for
1
2
3
for var=exp1,exp2,exp3 do
-- ...
end
var从exp1变化到exp2,每次递增exp3, exp可省略,默认为1
1
2
3
for i,v in ipairs(a) do
print(i, v)
end
repeat
1
2
3
4
5
a = 1
repeat
print(a)
a = a + 1
until( a > 10 )
if
1
2
3
4
5
6
7
if a == 1 then
print("true")
elseif a == 2 then
print("false")
else
print("else")
end
函数
1
2
3
4
5
6
7
function max(n1, n2)
if n1 > n2 then
return n1
else
return n2
end
end
多返回值
1
2
3
4
5
6
7
8
9
10
11
function max(arr)
local mi = 1
local max = arr[mi]
for i, v in ipairs(arr) do
if v > max then
max = v
mi = i
end
end
return mi, max
end
1
2
> max({1,4,6,78,0,4,34,54})
4 78
可变参数
1
2
3
4
5
6
7
function add(...)
local s = 0
for i, v in ipairs{...} do --> {...} 表示一个由所有变长参数构成的数组
s = s + v
end
return s
end
数组
1
2
-- Lua start array with index 1
arr = {1,2,3,4,5}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- 初始化二维数组
array = {}
for i=1,3 do
array[i] = {}
for j=1,3 do
array[i][j] = i*j
end
end
-- 访问二维数组
for i=1,3 do
for j=1,3 do
print(array[i][j])
end
end
OOP
Lua can also hide this parameter, using the colon operator.
1
2
3
function Account:withdraw (v)
self.balance = self.balance - v
end
and the method call as
1
a:withdraw(100.00)
The effect of the colon is to add an extra hidden parameter in a method definition and to add an extra argument in a method call.
The colon is only a syntactic facility, although a convenient one; there is nothing really new here. We can define a function with the dot syntax and call it with the colon syntax, or vice-versa, as long as we handle the extra parameter correctly:
1
2
3
4
5
6
7
8
9
10
11
12
13
Account = {
balance=0,
withdraw = function (self, v)
self.balance = self.balance - v
end
}
function Account:deposit (v)
self.balance = self.balance + v
end
Account.deposit(Account, 200.00)
Account:withdraw(100.00)
Lua does not have the concept of class; each object defines its own behavior and has a shape of its own. Nevertheless, it is not difficult to emulate classes in Lua, following the lead from prototype-based languages, such as Self and NewtonScript. In those languages, objects have no classes. Instead, each object may have a prototype, which is a regular object where the first object looks up any operation that it does not know about.To represent a class in such languages, we simply create an object to be used exclusively as a prototype for other objects (its instances).
Both classes and prototypes work as a place to put behavior to be shared by several objects.
1
2
3
4
5
6
function Account:new (o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end
1
2
a = Account:new{balance = 0}
a:deposit(100.00)
When we create this new account, a will have Account (the self in the call Account:new) as its metatable. Then, when we call a:deposit(100.00)
, we are actually calling a.deposit(a, 100.00)
(the colon is only syntactic sugar).
However, Lua cannot find a “deposit” entry in table a; so, it looks into the metatable’s __index entry. The situation now is more or less like this:
1
getmetatable(a).__index.deposit(a, 100.00)
That is, Lua calls the original deposit function, but passing a as the self parameter. So, the new account a inherited the deposit function from Account. By the same mechanism, it can inherit all fields from Account.
标准库
Lua standard libraries provide a rich set of functions that is implemented directly with the C API and is in-built with Lua programming language.
string
1
2
3
4
5
string.upper('hello')
string.lower('HELLO')
string.reverse("Lua")
string.format("%02d%02d",4,2)
string.len('hello')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 将整型数字转成字符并连接
string.char(arg)
-- 转换字符为整数值(可以指定某个字符,默认第一个字符)
string.byte(arg[,int])
-- 复制n次并连接
string.rep(string, n)
-- num 为替换次数,可忽略,默认为全部替换, 返回替换后的字符串和替换次数
string.gsub(mainString,findString,replaceString,num)
string.find (str, substr, [init, [end]])
-- 返回一个迭代器函数,每次调用返回下一个匹配的字符串,找不到时返回nil
string.gmatch(str, pattern)
-- 只寻找源字串str中的第一个匹配的字符串
string.match(str, pattern, init)
IDE
ZeroBrane Studio