Lua中的table函数库

来源:互联网 发布:知错改错善莫大焉 编辑:程序博客网 时间:2024/05/16 07:21

本文转自热血枫叶的博客原文地址http://blog.csdn.net/rexuefengye/article/details/20865997

Lua中的table函数库


      table库由一些操作table的辅助函数组成。他的主要作用之一是对Lua中array的大小给出一个合理的解释。另外还提供了一些从list中插入删除元素的函数,以及对array元素排序函数。


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


      concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组部分从start位置到end位置的所有元素, 元素间以指定的分隔符(sep)隔开。除了table外, 其他的参数都不是必须的, 分隔符的默认值是空字符, start的默认值是1, end的默认值是数组部分的总长.

      sep, start, end这三个参数是顺序读入的, 所以虽然它们都不是必须参数, 但如果要指定靠后的参数, 必须同时指定前面的参数.

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. test = {"Tom""Mary""Jam","Hey"}  
  2.   
  3.   
  4. print(table.concat(test, ":"))  
  5. print("***********************")  
  6. print(table.concat(test, nil, 1, 2))  
  7. print("***********************")  
  8. print(table.concat(test, "\n", 2, 3))  
  9.   
  10. print(table.maxn(test))  
运行结果

            


table.insert(table, pos, value)


      table.insert()函数在table的数组部分指定位置(pos)插入值为value的一个元素. pos参数可选, 默认为数组部分末尾.

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. network = {  
  2.     {name = "Tom" ,IP = "210.26.30.34"},  
  3.     {name = "Mary" ,IP = "210.26.30.23"},  
  4.     {name = "Jam" ,IP = "210.26.30.12"},  
  5.     {name = "hey" ,IP = "210.26.30.30"},  
  6. }  
  7. --添加数据  
  8. table.insert(network,{name = "Feng" ,IP = "210.26.30.11"})  

            


table.remove(table, pos)


      table.remove()函数删除并返回table数组部分位于pos位置的元素. 其后的元素会被前移. pos参数可选, 默认为table长度, 即从最后一个元素删起.

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. network = {  
  2.     {name = "Tom" ,IP = "210.26.30.34"},  
  3.     {name = "Mary" ,IP = "210.26.30.23"},  
  4.     {name = "Jam" ,IP = "210.26.30.12"},  
  5.     {name = "hey" ,IP = "210.26.30.30"},  
  6. }  
  7.   
  8.   
  9. --删除最后一条数据  
  10. table.remove(network,#network)  
            


table.sort(table, comp)


      table.sort(table, comp)
      table.sort()函数对给定的table进行升序排序.  comp是一个可选的参数, 此参数是一个外部函数, 可以用来自定义sort函数的排序标准.

      此函数应满足以下条件: 接受两个参数(依次为a, b), 并返回一个布尔型的值, 当a应该排在b前面时, 返回true, 反之返回false.

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. network = {"Tom","Jam","Mary"}  
  2. --升序  
  3. table.sort(network)  
  4. --降序  
  5. table.sort(network,function(a,b) return a > b end)  

            


      对于table.sort进行排序时,它还接受一个table并对其中的元素排序,如:升序、降序、按数字顺序、按符顺序或table中KEY的顺序。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. network = {  
  2.     {name = "Tom" ,IP = "210.26.30.34"},  
  3.     {name = "Mary" ,IP = "210.26.30.23"},  
  4.     {name = "Jam" ,IP = "210.26.30.12"},  
  5.     {name = "hey" ,IP = "210.26.30.30"},  
  6. }  
  7.   
  8. table.sort(network,function(a,b) return (a.IP < b.IP)end) --升序  

            


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


      会期望一个从 1(数字 1)开始的连续整数范围,遍历table中的key和value逐对进行function(i, v)操作

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};  
  2. table.foreachi(t1, function(i, v) print (i, "->",v) end)--等价于foreachi(t1, print)  

            


table.foreach(table, function(i, v))


      与foreachi不同的是,foreach会对整个表进行迭代。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};  
  2. table.foreach(t1, function(i, v) print (i,"->", v) end) ;  

            


table.maxn(table)


      table.maxn()函数返回指定table中所有正数key值中最大的key值. 如果不存在key值为正数的元素, 则返回0. 此函数不限于table的数组部分.

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. test = {"Tom""Mary""Jam","Hey"}--默认test[1] = "Tom",test[2] = "Mary",test[3] = "Jam",test[4] = "Hey"  
  2. print(table.maxn(test))  

            4


table.getn(table)


      返回table中元素的个数。注意:该table的key必须是有序的,索引是从1开始的, 若无序,无法得到正确的大小。如:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. t1 = {2, 4, 6, language="Lua", version="5", 8, 10, 12, web="hello lua"};  
  2. print(table.getn(t1))  
            6

      若table无序,可以这样得到大小:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. for k,v in pairs(t1) do  
  2.   
  3.     count = count + 1  
  4.   
  5. end  
  6. print(count)  

            9
0 0
原创粉丝点击