table库

table库是由一些辅助函数构成的,这些函数将table作为数组来操作。其中有用于在列表中插入和删除元素的函数,有对数组元素进行排序的函数,还有连接一个数组中的所有字符串的函数。

   1table的介绍

   table Lua里是一种重要的数据结构,它可以说是其他数据结构的基础,通常的数组、记录、线性表、队列、集合等数据结构都可以用table来表示,甚至连全局变量(_G)、模块、元表(metatable)等这些重要的Lua元素都是table的结构

   2table的特性

 1.table是一个“关联数组”,数组的索引可以是数字或者是字符串

 2.table的默认初始索引一般以 1开始

 3.table的变量只是一个地址引用,对 table的操作不会产生数据影响

 4.table不会固定长度大小,有新数据插入时长度会自动增长

 5.table的方法函数

   3concat

   函数 table.concat 主要用来把表里的每个元素通过一个分隔符(separator)连接组合起来,用法:table.concat(table, sep,  start, end)

   上面除 table 外,sepstartend这三个参数都是可选的,并且顺序读入的,如果没指定传入,函数concat会采用默认值(分隔符sep的默认值是空字符, start的默认值是1, end的默认值是数组部分的总长)去执行。

 local tbl = {"apple", "pear", "orange", "grape"}

 print(table.concat(tbl)) --applepearorangegrape

 print(table.concat(tbl, "")) --applepearorangegrape

 print(table.concat(tbl, "", 2)) --pearorangegrape

 print(table.concat(tbl, "", 2, 3)) --pearorange

对于密集型的字符并接,table.concat 比用 ".." 连接更高效

local str = {}

for i = 1, 10000 do

    str[i] = "str"

end

print(table.concat(str))

4insert

函数 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, "")) --applepearorangegrapewatermelon

 table.insert(tbl, 2, "watermelon")

 print(table.concat(tbl, "")) --applewatermelonpearorangegrapewatermelon

5maxn

函数 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

6pack

函数 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")

7remove

函数 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, ""))  --appleorangegrape

 table.remove(tbl)

 print(table.concat(tbl, ""))  --appleorange

8sort

函数 table.sort 用于对 table 里的元素作排序操作,用法:

 table.sort(table, comp)

参数 comp 是一个排序对比函数,它有两个参数param1param2,如果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, ""))  --pearorangegrapeapple

 local sort_func2 = function(a, b) return a < b end

 table.sort(tbl, sort_func2)

 print(table.concat(tbl, "")) --applegrapeorangepear

参数 comp 可选,缺省comp的情况下是对表作升序排序。

 local tbl = {"apple", "pear", "orange", "grape"}

 table.sort(tbl)

 print(table.concat(tbl, "")) --applegrapeorangepear

9unpack

函数 table.unpack 用于返回 table 里的元素,用法:

 table.unpack(table, start, end)

参数 start 是开始返回的元素位置,默认是1,参数end是返回最后一个元素的位置,默认是table最后一个元素的位置,参数startend都是可选

 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))

10table.foreachi(table, function(i, v))

会期望一个从 1(数字 1)开始的连续整数范围,遍历table中的keyvalue逐对进行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

11table.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

12table.getn(table)

返回table中元素的个数

t1 = {1, 2, 3, 5};

print(getn(t1))

->4

13table.setn(table, nSize)

设置table中的元素个数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值