Lua中pairs和ipairs有何本质区别,导致它们在遍历表时表现各异?
- 内容介绍
- 文章标签
- 相关推荐
本文共计874个文字,预计阅读时间需要4分钟。
Lua中pairs和ipairs的区别:
2012年5月11日 11:13:03 阅读数:98037
Lua标准库提供了集合并代器,包括:
- 迭代文件每一行的(io.lines)- 迭代table元素的(pairs)- 迭代数组元素的(ipairs)lua 中pairs 和 ipairs区别
2012年05月11日 11:13:03 阅读数:98037 lua 中pairs 和 ipairs区别标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的
(string.gmatch)等等。LUA手册中对与pairs,ipairs解释如下:
ipairs (t)
Returns three values: an iterator function, the tablet, and 0, so that the construction
for i,v in ipairs(t) dobodyend
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: thenextfunction, the tablet, andnil, so that the construction
for k,v in pairs(t) dobodyend
will iterate over all key–value pairs of tablet.
See functionnextfor the caveats of modifying the table during its traversal.
这样就可以看出 ipairs以及pairs 的不同。
pairs可以遍历表中所有的key,并且除了迭代器本身以及遍历表本身还可以返回nil;
但是ipairs则不能返回nil,只能返回数字0,如果遇到nil则退出。它只能遍历到表中出现的第一个不是整数的key
[cpp] view plain copy- 下面举个例子吧!
- eg:
- localtabFiles={
- [3]="test2",
- [6]="test3",
- [4]="test1"
- }
- fork,vinipairs(tabFiles)do
- print(k,v)
- end
- 猜测它的输出结果是什么呢?
- 根据刚才的分析,它在ipairs(tabFiles)遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。
- >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
- >Exitcode:0
- 那么,如果是
- fork,vinpairs(tabFiles)do
- print(k,v)
- end
- 则会输出所有:
- >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
- 3test2
- 6test3
- 4test1
- >Exitcode:0
- 现在改变一下表内容,
- localtabFiles={
- [1]="test1",
- [6]="test2",
- [4]="test3"
- }
- fork,vinipairs(tabFiles)do
- print(k,v)
- end
- 现在的输出结果显而易见就是key=1时的value值test1
- >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
- 1test1
- >Exitcode:0
- --[示例1.]--
- localtt=
- {
- [1]="test3",
- [4]="test4",
- [5]="test5"
- }
- fori,vinpairs(tt)do--输出"test4""test3""test5"
- print(tt[i])
- end
- fori,vinipairs(tt)do--输出"test3"k=2时断开
- print(tt[i])
- end
- --[[示例2.]]--
- tbl={"alpha","beta",[3]="uno",["two"]="dos"}
- fori,vinipairs(tbl)do--输出前三个
- print(tbl[i])
- end
- fori,vinpairs(tbl)do--全部输出
- print(tbl[i])
- end
www.cppblog.com/wc250en007/archive/2011/12/16/162203.html
本文共计874个文字,预计阅读时间需要4分钟。
Lua中pairs和ipairs的区别:
2012年5月11日 11:13:03 阅读数:98037
Lua标准库提供了集合并代器,包括:
- 迭代文件每一行的(io.lines)- 迭代table元素的(pairs)- 迭代数组元素的(ipairs)lua 中pairs 和 ipairs区别
2012年05月11日 11:13:03 阅读数:98037 lua 中pairs 和 ipairs区别标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的
(string.gmatch)等等。LUA手册中对与pairs,ipairs解释如下:
ipairs (t)
Returns three values: an iterator function, the tablet, and 0, so that the construction
for i,v in ipairs(t) dobodyend
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: thenextfunction, the tablet, andnil, so that the construction
for k,v in pairs(t) dobodyend
will iterate over all key–value pairs of tablet.
See functionnextfor the caveats of modifying the table during its traversal.
这样就可以看出 ipairs以及pairs 的不同。
pairs可以遍历表中所有的key,并且除了迭代器本身以及遍历表本身还可以返回nil;
但是ipairs则不能返回nil,只能返回数字0,如果遇到nil则退出。它只能遍历到表中出现的第一个不是整数的key
[cpp] view plain copy- 下面举个例子吧!
- eg:
- localtabFiles={
- [3]="test2",
- [6]="test3",
- [4]="test1"
- }
- fork,vinipairs(tabFiles)do
- print(k,v)
- end
- 猜测它的输出结果是什么呢?
- 根据刚才的分析,它在ipairs(tabFiles)遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。
- >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
- >Exitcode:0
- 那么,如果是
- fork,vinpairs(tabFiles)do
- print(k,v)
- end
- 则会输出所有:
- >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
- 3test2
- 6test3
- 4test1
- >Exitcode:0
- 现在改变一下表内容,
- localtabFiles={
- [1]="test1",
- [6]="test2",
- [4]="test3"
- }
- fork,vinipairs(tabFiles)do
- print(k,v)
- end
- 现在的输出结果显而易见就是key=1时的value值test1
- >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
- 1test1
- >Exitcode:0
- --[示例1.]--
- localtt=
- {
- [1]="test3",
- [4]="test4",
- [5]="test5"
- }
- fori,vinpairs(tt)do--输出"test4""test3""test5"
- print(tt[i])
- end
- fori,vinipairs(tt)do--输出"test3"k=2时断开
- print(tt[i])
- end
- --[[示例2.]]--
- tbl={"alpha","beta",[3]="uno",["two"]="dos"}
- fori,vinipairs(tbl)do--输出前三个
- print(tbl[i])
- end
- fori,vinpairs(tbl)do--全部输出
- print(tbl[i])
- end
www.cppblog.com/wc250en007/archive/2011/12/16/162203.html

