Lua table pair和ipair区别
官方描述:
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。
- local tabFiles = {
- [3] = "test2",
- [6] = "test3",
- [4] = "test1"
- }
- for k, v in ipairs(tabFiles) do
- print(k, v)
- end
猜测它的输出结果是什么呢?根据刚才的分析,它在 ipairs(tabFiles) 遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。
那么,如果是
- for k, v in pairs(tabFiles) do
- print(k, v)
- end
则会输出所有:
- >lua -e "io.stdout:setvbuf 'no'" "test.lua"
- 3 test2
- 6 test3
- 4 test1
- >Exit code: 0
现在改变一下表内容,
- local tabFiles = {
- [1] = "test1",
- [6] = "test2",
- [4] = "test3"
- }
- for k, v in ipairs(tabFiles) do
- print(k, v)
- end
现在的输出结果显而易见就是key=1时的value值test1。
更多:http://blog.csdn.net/tony7758/article/details/6334001
Lua table pair和ipair区别的更多相关文章
- 范性for语义以及pair和ipair的区别
详情参考 lua手册 1. 范性for语义 在了解pair和ipair前先简单了解下lua中的for循环,这里只阐述范性for循环的语义,范性 for 在自己内部保存迭代函数,实际上它保存三个值:迭代 ...
- lua 中pairs 和 ipairs区别
lua 中pairs 和 ipairs区别 标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 (s ...
- lua table integer index 特性
table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...
- 树形打印lua table表
为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...
- lua table 排序--满足多条件排序
前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...
- Lua中点和冒号的区别
在使用lua设计类时'.'和':'的区别主要在于使用'.'必须手动加self参数,使用':',可以隐藏这个参数,使用'.'调用使用':'定义的函数时,要注意,函数的第一个参数为self,如 funct ...
- cocos2d-x lua table数据存储
cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...
- cocos2d-x lua table与json的转换
cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...
- Lua table使用
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...
随机推荐
- Java——有关日期的方法
1.日期转换成String格式化输出: public String getDate() { SimpleDateFormat format = new SimpleDateFormat("y ...
- html-----007
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- C++多态性的理解
本文章转载来自:http://www.sollyu.com/?p=627 代码 #include <iostream.h> class Animal { public: void eat( ...
- CentOS7修改网卡为eth0
CentOS7修改网卡为eth0 1.编辑网卡信息 [root@linux-node2~]# cd /etc/sysconfig/network-scripts/ #进入网卡目录 [root@lin ...
- erlang 里的if 和 case
case Expression of Pattern1 [when Guard1] -> Expr_seq1; Pattern2 [when Guard2] -> Expr_seq2; … ...
- [java bug记录] java.util.zip.ZipException: invalid code lengths set
1. 描述:将代码迁移到maven工程,其中用ZipInputStream读取/src/main/resources下的zip文件时报错:“java.util.zip.ZipException: in ...
- ubuntu 在XP下硬盘安装
以下选择在XP下用 grub4dos 安装 ubuntu 12.04版本 需要下载两个文件:一个是grub4dos,另一个是 ubutuntu 镜像文件 grub4dos下载地址:http://dow ...
- 《工作型PPT设计之道》培训心得
参加包翔老师的“工作型PPT设计之道>培训,颇多心得,后来为部门新员工和同组同事做了转化培训,将心得整理成一份PPT讲义,效果颇佳.现将主要心得整理于此.因时间仓促,24条心得有拼凑之嫌,有待今 ...
- linux下测试磁盘的读写IO速度【转】
Reference1:http://server.chinabyte.com/495/12361995.shtmlReference2:https://www.deleak.com/blog/2011 ...