table的函数API

转载自http://dhq.me/lua-learning-notes-table

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

table 特性

在之前介绍 Lua 数据类型时,也说过了 table 的一些特性,简单列举如下(详情可查看之前的介绍):

  • table是一个“关联数组”,数组的索引可以是数字或者是字符串
  • table 的默认初始索引一般以 1 开始
  • table 的变量只是一个地址引用,对 table 的操作不会产生数据影响
  • table 不会固定长度大小,有新数据插入时长度会自动增长

table 的方法函数

Lua 5.2.2 内置有以下 7 中对 table 操作的方法:

concat

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

1
table.concat(table, sep,  start, end)

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

1
2
3
4
5
6
7
8
9
local tbl = {"apple""pear""orange""grape"}
 
print(table.concat(tbl))
 
print(table.concat(tbl, "、"))
 
print(table.concat(tbl, "、"2))
 
print(table.concat(tbl, "、"23))

对于密集型的字符并接,table.concat 比用 ".." 连接更高效,下面是用 time 测试的效果:

1
2
3
4
5
6
7
8
local str = {}
for i = 110000 do
    str[i] = "str"
end
print(table.concat(str))
--real  0m0.005s
--user  0m0.003s
--sys   0m0.002s
1
2
3
4
5
6
7
8
local str = ""
for i = 110000 do
    str = str .. "str"
end
print(str)
--real  0m0.041s
--user  0m0.037s
--sys   0m0.002s

insert

函数 table.insert 用于向 table 的指定位置(pos)插入一个新元素,用法:

1
table.insert(table, pos value)

参数 pos 是可选,默认是 table 末尾位置。

1
2
3
4
5
6
7
local tbl = {"apple""pear""orange""grape"}
 
table.insert(tbl, "watermelon")
print(table.concat(tbl, "、"))
 
table.insert(tbl, 2"watermelon")
print(table.concat(tbl, "、"))

maxn

函数 table.maxn 是返回 table 最大的正数索引值,用法:

1
table.maxn (table)

如果不存在正数的索引值,则返回 0。

1
2
3
4
5
6
7
8
local tbl = {"apple""pear""orange""grape"}
print(table.maxn(tbl))
 
local tbl = {"apple""pear""orange""grape", [26] = "watermelon"}
print(table.maxn(tbl))
 
local tbl = {[-1] = "apple", [-2] = "pear", [-3] = "orange", [-4] = "grape"}
print(table.maxn(tbl))

pack

函数 table.pack 是获取一个索引从 1 开始的参数表 table,并会对这个 table 预定义一个字段 n,表示该表的长度,用法:

1
table.pack(···)

该函数常用在获取传入函数的参数。

1
2
3
4
5
6
7
8
9
function table_pack(param, ...)
    local arg = table.pack(...)
    print("this arg table length is"arg.n)
    for i = 1arg.n do
        print(i, arg[i])
    end
end
 
table_pack("test""param1""param2""param3")

remove

函数 table.remove 用于删除 table 里某个值,用法:

1
table.remove (list [, pos])

参数 pos 可选,默认为删除 table 最后一个元素,并且参数 pos 的类型只能是数字 number 类型。

1
2
3
4
5
6
local tbl = {"apple""pear""orange""grape"}
table.remove(tbl, 2)
print(table.concat(tbl, "、"))
 
table.remove(tbl)
print(table.concat(tbl, "、"))

sort

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

1
table.sort(table, comp)

参数 comp 是一个排序对比函数,它有两个参数 param1、param2,如果 param1 排在 param2 前面,那么排序函数返回 true,否则返回 false。

1
2
3
4
5
6
7
8
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, "、"))
 
local sort_func2 = function(a, b) return a < b end
table.sort(tbl, sort_func2)
print(table.concat(tbl, "、"))

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

1
2
3
local tbl = {"apple""pear""orange""grape"}
table.sort(tbl)
print(table.concat(tbl, "、"))

unpack

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

1
table.unpack(table, start, end)

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

1
2
3
4
5
6
7
8
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, 23))

posted on 2013-12-21 17:39 cslover 阅读(406) 评论(0)  编辑 收藏 引用 所属分类: Lua


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2013年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜