码迷,mamicode.com
首页 > 其他好文 > 详细

Lua的Table表使用例子(便于使用查询)

时间:2014-06-25 23:41:33      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   color   

一、table.insert()

1.1

1     local countries = {"China", "England", "Brazil"}
2     --尾插法(Pos不填,默认插入尾部)
3     table.insert(countries, "France")
4     --头插法(首部插入)
5     table.insert(countries, 1, "Australia") 
6     
7     dump(countries)

bubuko.com,布布扣

二、table.remove()

2.1

1     local countries = {"China", "England", "Brazil", "England"}
2     table.removeItem(countries, "England", true)
3     dump(countries)

bubuko.com,布布扣

2.2

1     local countries = {"China", "England", "Brazil", "Australia"}
2     --移除位置2上的元素(“England”)
3     table.remove(countries, 2)
4     --默认尾部删除元素
5     table.remove(countries)
6     dump(countries)

bubuko.com,布布扣

三、table.maxn()

3.1

1     local countries = {"China", "England", "Brazil", [7] = "Australia"}
2     local Cars = {[-3] = "Benz", [-2] = "Toyota", [-1] ="LandRover"}
3     --获取table的最大下标。如下标小于0,则返回0
4     print("Countries Maxn  >> "..table.maxn(countries))
5     print("Cars Maxn  >> "..table.maxn(Cars))
6     dump(countries)

bubuko.com,布布扣

四、table.concat()

4.1

1     local countries = {"China", "England", "Brazil","Australia"}
2     --用“+”拼接table中的元素(table[1].."+"..table[2]..………….."+"..table[n])
3     print("拼接:  "..table.concat(countries, "+"))
4     print("从Pos[3]开始拼接:  "..table.concat(countries, "+", 3))

bubuko.com,布布扣

4.2

1     local countries = {"China", "England", "Brazil","Australia"}
2     --table.concat(table, "X", began, end)
3     --从began的位置,用"X"连接到end位置结束
4     print("拼接:  "..table.concat(countries, "+", 2, 3))

bubuko.com,布布扣

五、table.sort()

5.1 

1     local countries = {"China", "England", "Brazil","Australia"}
2     table.sort(countries)
3     dump(countries)

bubuko.com,布布扣

5.2

 1     local countries = {"China", "England", "Brazil","Australia"}
 2     --自定义排序方式
 3     myComparator = function(a, b) 
 4         if a > b then
 5             return true
 6         else 
 7             return false
 8         end
 9     end 
10     table.sort(countries, myComparator)
11     dump(countries)

bubuko.com,布布扣

 

Lua的Table表使用例子(便于使用查询),布布扣,bubuko.com

Lua的Table表使用例子(便于使用查询)

标签:style   class   blog   code   http   color   

原文地址:http://www.cnblogs.com/vokie/p/3804462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!