12使用nil删除变量

1
2
3
4
5
6
7
8
9
10
11
12
tab1 = { key1 = "val1", key2 = "val2", "val3" }
for k, v in pairs(tab1) do
-- ..字符串连接符,就是python里的+
print(k .. " - " .. v)
end

print('--------')

tab1.key1 = nil
for k, v in pairs(tab1) do
print(k .. " - " .. v)
end

21如何初始化table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
local colorTable = {} 
colorTable[1] = "blue"
colorTable[2] = "red"
colorTable[3] = "yellow"
colorTable[4] = "green"
colorTable[5] = "purple"
print(colorTable[3])
-------------------------------
local colorTable = {
[1] = "blue",
[2] = "red",
[3] = "yellow",
[4] = "green",
[5] = "purple"
}
print(colorTable[3])
-------------------------------
local colorTable = {
"blue",
"red",
"yellow",
"green",
"purple"
}
print(colorTable[3])
-------------------------------
local colorTable = {"blue","red","yellow","green","purple"}
print(colorTable[3])

25(1)递归的斐波那契函数

1
2
3
4
5
6
7
8
9
10
function factorial1(n)
if n == 0 then
return 1
else
return n * factorial1(n - 1)
end
end
print(factorial1(5))
factorial2 = factorial1
print(factorial2(5))

25(2)把函数作为变量引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 此函数接受一个func函数作为参数
function testFun(tab,func)
for k ,v in pairs(tab) do
print(func(k,v))
end
end

tab={key1="val1",key2="val2"}

testFun(tab,
function(key,val)--匿名函数
return key.."="..val
end
)

28变量作用域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
a = 5               -- 全局变量
local b = 5 -- 局部变量

function joke()
c = 5 -- 全局变量
local d = 6 -- 局部变量
end
-- lua和python一样,函数也是懒加载
-- 只有运行了,变量才会被定义
print(c,d)
joke()
print('-----')
print(c,d)
print('-----')

do
local a = 6 -- 局部变量
b = 6 -- 对局部变量重新赋值
print(a,b);
end
print(a,b)

39函数固定参数和可变参数混用

1
2
3
4
5
6
7
-- 固定参数、可变参数混用
function fwrite(fmt, ...) ---> 固定的参数fmt
return io.write(string.format(fmt, ...))
end

fwrite("runoob\n") --->fmt = "runoob", 没有变长参数。
fwrite("%d%d\n", 1, 2) --->fmt = "%d%d", 变长参数为 1 和 2

48string类型的函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
--upper和lower
print(string.upper('a'),string.lower('A'))
-- 替换:在aaaa中找a,将其替换为z,替换三次
print(string.gsub("aaaa","a","z",3))
-- 从Hello Lua user找Lua,从第一位开始找
-- 返回匹配到的索引值
print(string.find("Hello Lua user", "Lua", 1))
-- 反转
print(string.reverse("Lua"))
-- format
print(string.format("the value is:%s",'hyl'))
print(string.format("the value is:%d",4))
-- 二进制转换
print(string.char(97,98,99,100))
print(string.byte("ABCD",4))
-- 长度
print(string.len("abc"))
-- 字符串数量翻倍
print(string.rep("abcd",3))
-- 连接字符串
print("www.runoob".."com")

49string的匹配函数

1
2
3
4
5
6
7
8
9
10
do  
function foo(...)
for i = 1, select('#', ...) do -->获取参数总数
local arg = select(i, ...); -->读取参数
print("arg", arg);
end
end

foo(1, 2, 3, 4);
end

50string的字符串格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
string1 = "Lua"
string2 = "Tutorial"
number1 = 10
number2 = 20
-- 基本字符串格式化
print(string.format("基本格式化 %s %s",string1,string2))
-- 日期格式化
date = 2; month = 1; year = 2014
print(string.format("日期格式化 %02d/%02d/%03d", date, month, year))
-- 十进制格式化
print(string.format("%.4f",1/3))


string.format("%c", 83) --输出S
string.format("%+d", 17.0) --输出+17
string.format("%05d", 17) --输出00017
string.format("%o", 17) --输出21
string.format("%u", 3.14) --输出3
string.format("%x", 13) --输出d
string.format("%X", 13) --输出D
string.format("%e", 1000) --输出1.000000e+03
string.format("%E", 1000) --输出1.000000E+03
string.format("%6.3f", 13) --输出13.000
string.format("%q", "One\nTwo") --输出"One\
  --Two"
string.format("%s", "monkey") --输出monkey
string.format("%10s", "monkey") --输出 monkey
string.format("%5.3s", "monkey") --输出 mon

51string使用char和byte实现字符转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 字符转换
-- 转换第一个字符
print(string.byte("Lua"))
-- 转换第三个字符
print(string.byte("Lua",3))
-- 转换末尾第一个字符
print(string.byte("Lua",-1))
-- 第二个字符
print(string.byte("Lua",2))
-- 转换末尾第二个字符
print(string.byte("Lua",-2))

-- 整数 ASCII 码转换为字符
print(string.char(97))

57多维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- 初始化数组
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


-- 初始化数组
array = {}
maxRows = 3
maxColumns = 3
for row=1,maxRows do
for col=1,maxColumns do
array[row*maxColumns +col] = row*col
end
end

-- 访问数组
for row=1,maxRows do
for col=1,maxColumns do
print(array[row*maxColumns +col])
end
end

61无状态迭代器

1
2
3
4
5
6
7
8
9
10
11
12
function square(iteratorMaxCount,currentNumber)
if currentNumber<iteratorMaxCount
then
currentNumber = currentNumber+1
return currentNumber, currentNumber*currentNumber
end
end

for i,n in square,3,0
do
print(i,n)
end

62多状态迭代器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
array = {"Lua", "Tutorial"}

function elementIterator (collection)
local index = 0
local count = #collection
-- 闭包函数
return function ()
index = index + 1
if index <= count
then
-- 返回迭代器的当前元素
return collection[index]
end
end
end

for element in elementIterator(array)
do
print(element)
end

65(1)table连接concat

1
2
3
4
5
6
7
8
9
fruits = {"banana","orange","apple"}
-- 返回 table 连接后的字符串
print("连接后的字符串 ",table.concat(fruits))

-- 指定连接字符
print("连接后的字符串 ",table.concat(fruits,", "))

-- 指定索引来连接 table
print("连接后的字符串 ",table.concat(fruits,", ", 2,3))

65(2)table插入删除insert,remove

1
2
3
4
5
6
7
8
9
10
11
12
13
fruits = {"banana","orange","apple"}

-- 在末尾插入
table.insert(fruits,"mango")
print("索引为 4 的元素为 ",fruits[4])

-- 在索引为 2 的键处插入
table.insert(fruits,2,"grapes")
print("索引为 2 的元素为 ",fruits[2])

print("最后一个元素为 ",fruits[5])
table.remove(fruits)
print("移除后最后一个元素为 ",fruits[5])

65(3)table排序sort

1
2
3
4
5
6
7
8
9
10
11
fruits = {"banana","orange","apple","grapes"}
print("排序前")
for k,v in ipairs(fruits) do
print(k,v)
end

table.sort(fruits)
print("排序后")
for k,v in ipairs(fruits) do
print(k,v)
end

65(4)table最大值maxn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function table_maxn(t)
local mn=nil;
for k, v in pairs(t) do
if(mn==nil) then
mn=v
end
if mn < v then
mn = v
end
end
return mn
end
tbl = {[1] = 2, [2] = 6, [3] = 34, [26] =5}
print("tbl 最大值:", table_maxn(tbl))
print("tbl 长度 ", #tbl)

66模块导入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- 文件名为 training1.lua
-- 定义一个名为 training1 的模块
training1 = {}
-- 定义一个常量
training1.constant = "这是一个常量"
-- 定义一个函数
function training1.func1()
io.write("这是一个公有函数!\n")
end

local function func2()
print("这是一个私有函数!")
end

function training1.func3()
func2()
end

return training1

74协程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
co = coroutine.create(
function(i)
print(i);
end
)

coroutine.resume(co, 1) -- 1
print(coroutine.status(co)) -- dead

print("----------")

co = coroutine.wrap(
function(i)
print(i);
end
)

co(1)

print("----------")

co2 = coroutine.create(
function()
for i=1,10 do
print(i)
if i == 3 then
print(coroutine.status(co2)) --running
print(coroutine.running()) --thread:XXXXXX
end
coroutine.yield()
end
end
)

coroutine.resume(co2) --1
coroutine.resume(co2) --2
coroutine.resume(co2) --3

print(coroutine.status(co2)) -- suspended
print(coroutine.running())

print("----------")

78简单模式的文件IO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- 以只读方式打开文件
file = io.open("test.lua", "r")

-- 设置默认输入文件为 test.lua
io.input(file)

-- 输出文件第一行
print(io.read())

-- 关闭打开的文件
io.close(file)

-- 以附加的方式打开只写文件
file = io.open("test.lua", "a")

-- 设置默认输出文件为 test.lua
io.output(file)

-- 在文件最后一行添加 Lua 注释
io.write("-- test.lua 文件末尾注释")

-- 关闭打开的文件
io.close(file)

81完全模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
file = io.open("test.lua", "r")

-- 输出文件第一行
print(file:read())

-- 关闭打开的文件
file:close()

-- 以附加的方式打开只写文件
file = io.open("test.lua", "a")

-- 在文件最后一行添加 Lua 注释
file:write("--test")

-- 关闭打开的文件
file:close()