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 ...
随机推荐
- Android中去掉标题栏的3种方法
1.在java代码中 (SplashActivity继承AppCompatActivity时无效)
- Linq 的IQueryable和IEnumerable方式
IEnumerable方式: public IEnumerable<WebManageUsers> GetWebManageUsers(ISpecification<WebManag ...
- ASP.NET MVC5总结(三)登陆中常用技术解析之session与cookie
1.session机制 session机制是在服务器端保持状态的方案,在做系统登陆时,我们往往会用到session来存储一些用户登录的重要信息,而这些信息是不能存在cookie中的. 当访问量增多时, ...
- 利用Highcharts制作web图表学习(一)
前言:最近项目中需要对数据进行汇总和分析得出柱状图或曲线图这样散装点图,经过学习和测试发现Highchart不错,如果大家项目中需要的话,不妨看看有的例子摘自官网 一.先说说HighCharts的 ...
- html-----010
22 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- JavaScript之String()和.toString()
JS中 转换字符串的方法有两个 一个String(),一个.toString(). 通常情况下 这两种使用没有太大的区别.但是需要注意几点: undefined: toString() var tes ...
- 安装"MySQLdb"一波三折.
在慕课网学习课程"Python操作MySQL数据库",安装"MySQLdb"时遇到问题. 先是找错地方: 百度搜索"Mysql for Python& ...
- asmdisk 丢失问题一次记录
环境 vm12 workstation ,11.2R 在安装RAC 第二台机器不显示磁盘的是问题 , oracleasm listdisks 查询没有结果 , 于是执行 oracleasm scand ...
- jquery 插件的编写
/** * 插件的学习 * 原文地址:http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html#home */ ;(function($, ...
- js判断是否全是相同的字符串
isSameStr("aa2a") //不都是相同的字符 function isSameStr(str){ var tem=0; for(var i=0;i<str.leng ...