官方描述:

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 next 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.

See function next for the caveats of modifying the table during its traversal.

这样就可以看出 ipairs以及pairs 的不同。pairs可以遍历表中所有的key,并且除了迭代器本身以及遍历表本身还可以返回nil;但是ipairs则不能返回nil,只能返回数字0,如果遇到nil则退出。它只能遍历到表中出现的第一个不是整数的key。

  1. local tabFiles = {
  2. [3] = "test2",
  3. [6] = "test3",
  4. [4] = "test1"
  5. }
  6. for k, v in ipairs(tabFiles) do
  7. print(k, v)
  8. end

猜测它的输出结果是什么呢?根据刚才的分析,它在 ipairs(tabFiles) 遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。

  1. >lua -e "io.stdout:setvbuf 'no'" "test.lua"
  2. >Exit code: 0

那么,如果是

  1. for k, v in pairs(tabFiles) do
  2. print(k, v)
  3. end

则会输出所有:

  1. >lua -e "io.stdout:setvbuf 'no'" "test.lua"
  2. 3 test2
  3. 6 test3
  4. 4 test1
  5. >Exit code: 0

现在改变一下表内容,

  1. local tabFiles = {
  2. [1] = "test1",
  3. [6] = "test2",
  4. [4] = "test3"
  5. }
  6. for k, v in ipairs(tabFiles) do
  7. print(k, v)
  8. end

现在的输出结果显而易见就是key=1时的value值test1。

更多:http://blog.csdn.net/tony7758/article/details/6334001

Lua table pair和ipair区别的更多相关文章

  1. 范性for语义以及pair和ipair的区别

    详情参考 lua手册 1. 范性for语义 在了解pair和ipair前先简单了解下lua中的for循环,这里只阐述范性for循环的语义,范性 for 在自己内部保存迭代函数,实际上它保存三个值:迭代 ...

  2. lua 中pairs 和 ipairs区别

    lua 中pairs 和 ipairs区别 标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 (s ...

  3. lua table integer index 特性

    table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...

  4. 树形打印lua table表

    为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...

  5. lua table 排序--满足多条件排序

    前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...

  6. Lua中点和冒号的区别

    在使用lua设计类时'.'和':'的区别主要在于使用'.'必须手动加self参数,使用':',可以隐藏这个参数,使用'.'调用使用':'定义的函数时,要注意,函数的第一个参数为self,如 funct ...

  7. cocos2d-x lua table数据存储

    cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...

  8. cocos2d-x lua table与json的转换

    cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...

  9. Lua table使用

    days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...

随机推荐

  1. Unity3D 之UGUI 滑动条(Slider)

    这里来讲解下UGUI 滑动条(Slider)的用法 控件下面有三个游戏对象 Background -->背景 Fill Area --> 前景区域 Handle Slide Area -- ...

  2. java ssm框架入门(一)面向接口编程

    因为工作上用到spring + strtus2 + mybatis ,所以开始学习下这个框架. 这里用到的是MySQL数据库 首先从web.xml 开始 <?xml version=" ...

  3. Python入门 学习笔记 (二)

      今天学习了一些简单的语法规则,话不多说,开始了:      二.数据类型                常用数据类型中的整形和浮点型就不多说了.                1.字符串     ...

  4. 在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法

    在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法   ==========方法1: --------1. 选相应 ...

  5. Erlang官方站点

    YOUR ERLANG COMMUNITY SITE Welcome to erlangcentral.org, the Erlang community site where you can rea ...

  6. IOS 学习笔记 2015-04-15 Xcode 工程模板分类

    一 Application类型    我们大部分呢的开发工作都是使用Application类型的模板创建IOS程序开始的,该类型包括5个模板1 Master-Detail-Application    ...

  7. MySQL的基本使用

    SQL     DDL:数据定义语言  CREATE DROP ALTER     DML:数据操作语言  SELECT INSERT UPDATE DELETE     DCL:数据控制语言  GR ...

  8. ubuntu 安装qq

    受不了webqq那个界面 ,各种不习惯 .今天在ubuntu 12.04LTS 版本中 ,终于装上了qq2012,下面介绍一下安装方法 1  安装 wine sudo  apt-get install ...

  9. ubuntu samba共享安装 配置

    参考: http://www.360doc.com/content/11/0615/12/3989678_127081905.shtml 参考: http://xfshean.blog.163.com ...

  10. Spring Cloud Eureka Server 启停状态监控

    目前发现如下的api: 当时没有找到文档 http://localhost:8761/eureka/apps 参考文章:(此文中api带有v2我自己试验不需要v2) http://blog.csdn. ...