lua中pairs和ipairs的区别
标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的
(string.gmatch)等等。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 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
下面举个例子吧! eg:
local tabFiles = {
[] = "test2",
[] = "test3",
[] = "test1"
} for k, v in ipairs(tabFiles) do
print(k, v)
end 猜测它的输出结果是什么呢? 根据刚才的分析,它在 ipairs(tabFiles) 遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。 >lua -e "io.stdout:setvbuf 'no'" "Test.lua"
>Exit code: 那么,如果是
for k, v in pairs(tabFiles) do
print(k, v)
end
则会输出所有 :
>lua -e "io.stdout:setvbuf 'no'" "Test.lua"
test2
test3
test1
>Exit code:
现在改变一下表内容,
local tabFiles = {
[] = "test1",
[] = "test2",
[] = "test3"
}
for k, v in ipairs(tabFiles) do
print(k, v)
end
现在的输出结果显而易见就是key=1时的value值test1
>lua -e "io.stdout:setvbuf 'no'" "Test.lua"
test1
>Exit code:
--[示例1.]--
local tt =
{
[] = "test3",
[] = "test4",
[] = "test5"
} for i,v in pairs(tt) do -- 输出 "test4" "test3" "test5"
print( tt[i] )
end for i,v in ipairs(tt) do -- 输出 "test3" k=2时断开
print( tt[i] )
end -- [[示例2.]] --
tbl = {"alpha", "beta", [] = "uno", ["two"] = "dos"} for i,v in ipairs(tbl) do --输出前三个
print( tbl[i] )
end for i,v in pairs(tbl) do --全部输出
print( tbl[i] )
end
转自http://blog.csdn.net/witch_soya/article/details/7556595
lua中pairs和ipairs的区别的更多相关文章
- Lua中 pairs和ipairs的区别
Lua系列–pairs和ipairsLua中Table的存储方式在看二者的区别之前,我们首先来看一下Lua中的table是如何在内存中进行分配的.Table的组成:1.哈希表 用来存储Key-Valu ...
- Lua 中 pairs 和 ipairs 的区别
ipairs (t) Returns three values: an iterator function, the table t, and 0, so that the construction ...
- lua中pairs 和 ipairs 的区别
1.table中存储值的时候,是按照顺序存储的,存储 k-v 的时候,是按照 k 的哈希值存储的. 2.ipairs --- 只能输出 table 中的值,并且不可输出nil,遇到 ni l就退出 p ...
- lua 中pairs 和 ipairs区别
lua 中pairs 和 ipairs区别 标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 (s ...
- Lua 中pairs与ipairs区别
local tmp_tab = {}; tmp_tab[]="lua"; tmp_tab[]="hello" tmp_tab[]="aaa" ...
- lua 中pairs 和 ipairs差别
ipairs 和pairs在lua中都是遍历tbale的函数可是两者有差别 1.pairs遍历table中的全部的key-vale 而ipairs会依据key的数值从1開始加1递增遍历相应的table ...
- Lua-泛型for循环 pairs和ipairs的区别
先看一段简单的代码: local mytable = { , , aa = "abc", subtable = {}, , } --for循环1 print("for - ...
- pairs 和 ipairs 的区别
ipairs 在迭代过程中是会直接跳过所有手动设定key值的变量.pairs不会跳过手动设置key值的变量. 实例 tab = {,,a="cd","d"} f ...
- 【Lua】Lua中ipair和pair的区别
pairs会遍历table的所有键值对. 而ipairs就是固定地从key值1开始,下次key累加1进行遍历,如果key对应的value不存在,就停止遍历.顺便说下,记忆也很简单,带i的就是根据int ...
随机推荐
- [Angular 2] Create template with Params
Angular 2 templates have a special let syntax that allows you to define and pass a context when they ...
- linux下网络编程常见问题
网络程序异常退出无core文件产生 这种情况发生在一边连接端已经关闭,但是另外一边还在对连接句柄做send操作,这样做send操作的进程会收到SIGPIPE信号,默认行为是直接退出且不会产生core. ...
- PHP Fuzzing行动——源码审计
目录: Section 1: 20种PHP源码快速审计方式 Section 2: PHP源码审计自动化( PHP Fuzzer ) 风险级别: ■ Low ■ Medium ■ High 在开 ...
- tachyon 本地模式安装
本地模式不用考虑hadoop的版本,所以直接下载 binary 包或者自己编译 1.配置主机名.JDK.关闭防火墙.关闭Selinux.配置hosts ... ... 2.设置本机SSH免密码登陆 . ...
- BootStrap2学习日记17---导航路径和分页
导航路径 代码: <ul class="breadcrumb"> <li><a href="#">Home</a> ...
- 【阿里云产品公测】阿里云ACE部署通用完整教程及评测
[阿里云产品公测]阿里云ACE部署通用完整教程及评测 作者:阿里云用户bailimei ACE应该是目前在公测的服务中应用最广泛的一项服务.在公测云引擎ACE前曾使用过新浪SAE,而ACE给我的最初印 ...
- 【Linux/Ubuntu学习1】Linux /etc 目录详解
/etc目录 包含很多文件.许多网络配置文件也在/etc 中. /etc/rc or/etc/rc.d or/etc/rc*.d 启动.或改变运行级时运行的scripts或scripts的 ...
- 《算法导论》习题解答 Chapter 22.1-2(邻接矩阵与链表)
链表如图: 矩阵: 1 2 3 4 5 6 7 1 0 1 1 0 0 0 0 2 1 0 0 1 1 0 0 3 1 0 0 0 0 1 1 4 0 1 0 0 0 0 0 5 0 1 0 0 0 ...
- ASCII码表 char(9),char(10),char(13)等
char(9) 水平制表符 char(10) 换行 char(13) 回车 测试ASCII码的方法: 在记事本中,按住ALT键,同时用小键盘输入十进制的ASCII码,然后松手,就可以看到效果了! ...
- String类的方法2
---恢复内容开始--- .ToLower() //转为小写字符串"AbC"-->"abc" .ToUpper() //转为大写"A ...