Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1018284
  • 博文数量: 297
  • 博客积分: 11721
  • 博客等级: 上将
  • 技术积分: 3431
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 10:21
文章分类

全部博文(297)

文章存档

2016年(9)

2011年(71)

2010年(137)

2009年(80)

分类: LINUX

2016-03-11 19:33:58

LUA手册中对与pairs,ipairs解释如下:

ipairs (t)

Returns three values: an iterator function, the table t, and 0, so that the construction

for i,v in ipairs(t) do body end

will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table.

  

pairs (t)

Returns three values: the  function, the table t, and nil, so that the construction

for k,v in pairs(t) do body end

will iterate over all key–value pairs of table t.


如:

 

website= {"", "", ["baidu"] = "", ["google"] = ""}


for key, value in ipairs(website) do


        print(key, value)


end



-pairs()函数基本和ipairs()函数用法相同, 区别在于:


pairs()可以遍历整个table,即包括数组及非数组部分。


-->如有pairs迭代输出如下:


-->1


-->2  


-->baidu  


-->google  



ipairs()函数用于遍历table中的数组部分。


-->如有ipairs迭代输出如下:


-->1


-->2  

阅读(2474) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~