table库是由一些辅助函数构成的,这些函数将table作为数组来操作。其中有用于在列表中插入和删除元素的函数,有对数组元素进行排序的函数,还有连接一个数组中的所有字符串的函数。
1、table的介绍
table 在 Lua里是一种重要的数据结构,它可以说是其他数据结构的基础,通常的数组、记录、线性表、队列、集合等数据结构都可以用table来表示,甚至连全局变量(_G)、模块、元表(metatable)等这些重要的Lua元素都是table的结构
2、table的特性
1.table是一个“关联数组”,数组的索引可以是数字或者是字符串
2.table的默认初始索引一般以 1开始
3.table的变量只是一个地址引用,对 table的操作不会产生数据影响
4.table不会固定长度大小,有新数据插入时长度会自动增长
5.table的方法函数
3、concat
函数 table.concat 主要用来把表里的每个元素通过一个分隔符(separator)连接组合起来,用法:table.concat(table, sep, start, end)
上面除 table 外,sep、start、end这三个参数都是可选的,并且顺序读入的,如果没指定传入,函数concat会采用默认值(分隔符sep的默认值是空字符, start的默认值是1, end的默认值是数组部分的总长)去执行。
local tbl = {"apple", "pear", "orange", "grape"}
print(table.concat(tbl)) --applepearorangegrape
print(table.concat(tbl, "、")) --apple、pear、orange、grape
print(table.concat(tbl, "、", 2)) --pear、orange、grape
print(table.concat(tbl, "、", 2, 3)) --pear、orange
对于密集型的字符并接,table.concat 比用 ".." 连接更高效
local str = {}
for i = 1, 10000 do
str[i] = "str"
end
print(table.concat(str))
4、insert
函数 table.insert 用于向 table 的指定位置(pos)插入一个新元素,用法:
table.insert(table, pos value)
参数 pos 是可选,默认是table末尾位置。
local tbl = {"apple", "pear", "orange", "grape"}
table.insert(tbl, "watermelon")
print(table.concat(tbl, "、")) --apple、pear、orange、grape、watermelon
table.insert(tbl, 2, "watermelon")
print(table.concat(tbl, "、")) --apple、watermelon、pear、orange、grape、watermelon
5、maxn
函数 table.maxn 是返回 table 最大的正数索引值,用法:
table.maxn (table)
如果不存在正数的索引值,则返回 0。
local tbl = {"apple", "pear", "orange", "grape"}
print(table.maxn(tbl)) --4
local tbl = {"apple", "pear", "orange", "grape", [26] = "watermelon"}
print(table.maxn(tbl)) --26
local tbl = {[-1] = "apple", [-2] = "pear", [-3] = "orange", [-4] = "grape"}
print(table.maxn(tbl)) --0
6、pack
函数 table.pack 是获取一个索引从 1 开始的参数表table,并会对这个table预定义一个字段n,表示该表的长度,用法:
table.pack(···)
该函数常用在获取传入函数的参数。
function table_pack(param, ...)
local arg = table.pack(...)
print("this arg table length is", arg.n)
for i = 1, arg.n do
print(i, arg[i])
end
end
table_pack("test", "param1", "param2", "param3")
7、remove
函数 table.remove 用于删除 table 里某个值,用法:
table.remove (list [, pos])
参数 pos 可选,默认为删除table最后一个元素,并且参数pos的类型只能是数字number类型。
local tbl = {"apple", "pear", "orange", "grape"}
table.remove(tbl, 2)
print(table.concat(tbl, "、")) --apple、orange、grape
table.remove(tbl)
print(table.concat(tbl, "、")) --apple、orange
8、sort
函数 table.sort 用于对 table 里的元素作排序操作,用法:
table.sort(table, comp)
参数 comp 是一个排序对比函数,它有两个参数param1、param2,如果param1排在param2前面,那么排序函数返回true,否则返回false。
local tbl = {"apple", "pear", "orange", "grape"}
local sort_func1 = function(a, b) return a > b end
table.sort(tbl, sort_func1)
print(table.concat(tbl, "、")) --pear、orange、grape、apple
local sort_func2 = function(a, b) return a < b end
table.sort(tbl, sort_func2)
print(table.concat(tbl, "、")) --apple、grape、orange、pear
参数 comp 可选,缺省comp的情况下是对表作升序排序。
local tbl = {"apple", "pear", "orange", "grape"}
table.sort(tbl)
print(table.concat(tbl, "、")) --apple、grape、orange、pear
9、unpack
函数 table.unpack 用于返回 table 里的元素,用法:
table.unpack(table, start, end)
参数 start 是开始返回的元素位置,默认是1,参数end是返回最后一个元素的位置,默认是table最后一个元素的位置,参数start、end都是可选
local tbl = {"apple", "pear", "orange", "grape"}
print(table.unpack(tbl))
local a, b, c, d = table.unpack(tbl)
print(a, b, c, d)
print(table.unpack(tbl, 2))
print(table.unpack(tbl, 2, 3))
10、table.foreachi(table, function(i, v))
会期望一个从 1(数字 1)开始的连续整数范围,遍历table中的key和value逐对进行function(i, v)操作
t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};
table.foreachi(t1, function(i, v) print (i, v) end) ; --等价于foreachi(t1, print)
输出结果:
1 2
2 4
3 6
4 8
5 10
6 12
11、table.foreach(table, function(i, v))
与foreachi不同的是,foreach会对整个表进行迭代
t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};
table.foreach(t1, function(i, v) print (i, v) end) ;
输出结果:
1 2
2 4
3 6
4 8
5 10
12、table.getn(table)
返回table中元素的个数
t1 = {1, 2, 3, 5};
print(getn(t1))
->4
13、table.setn(table, nSize)
设置table中的元素个数