Lua中pairs和ipairs有何本质区别,导致它们在遍历表时表现各异?

2026-04-01 18:451阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计874个文字,预计阅读时间需要4分钟。

Lua中pairs和ipairs有何本质区别,导致它们在遍历表时表现各异?

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.

Lua中pairs和ipairs有何本质区别,导致它们在遍历表时表现各异?

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
  1. 下面举个例子吧!
  2. eg:
  3. localtabFiles={
  4. [3]="test2",
  5. [6]="test3",
  6. [4]="test1"
  7. }
  8. fork,vinipairs(tabFiles)do
  9. print(k,v)
  10. end
  11. 猜测它的输出结果是什么呢?
  12. 根据刚才的分析,它在ipairs(tabFiles)遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。
  13. >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
  14. >Exitcode:0
  15. 那么,如果是
  16. fork,vinpairs(tabFiles)do
  17. print(k,v)
  18. end
  19. 则会输出所有:
  20. >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
  21. 3test2
  22. 6test3
  23. 4test1
  24. >Exitcode:0
  25. 现在改变一下表内容,
  26. localtabFiles={
  27. [1]="test1",
  28. [6]="test2",
  29. [4]="test3"
  30. }
  31. fork,vinipairs(tabFiles)do
  32. print(k,v)
  33. end
  34. 现在的输出结果显而易见就是key=1时的value值test1
  35. >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
  36. 1test1
  37. >Exitcode:0
  38. --[示例1.]--
  39. localtt=
  40. {
  41. [1]="test3",
  42. [4]="test4",
  43. [5]="test5"
  44. }
  45. fori,vinpairs(tt)do--输出"test4""test3""test5"
  46. print(tt[i])
  47. end
  48. fori,vinipairs(tt)do--输出"test3"k=2时断开
  49. print(tt[i])
  50. end
  51. --[[示例2.]]--
  52. tbl={"alpha","beta",[3]="uno",["two"]="dos"}
  53. fori,vinipairs(tbl)do--输出前三个
  54. print(tbl[i])
  55. end
  56. fori,vinpairs(tbl)do--全部输出
  57. print(tbl[i])
  58. end

www.cppblog.com/wc250en007/archive/2011/12/16/162203.html

本文共计874个文字,预计阅读时间需要4分钟。

Lua中pairs和ipairs有何本质区别,导致它们在遍历表时表现各异?

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.

Lua中pairs和ipairs有何本质区别,导致它们在遍历表时表现各异?

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
  1. 下面举个例子吧!
  2. eg:
  3. localtabFiles={
  4. [3]="test2",
  5. [6]="test3",
  6. [4]="test1"
  7. }
  8. fork,vinipairs(tabFiles)do
  9. print(k,v)
  10. end
  11. 猜测它的输出结果是什么呢?
  12. 根据刚才的分析,它在ipairs(tabFiles)遍历中,当key=1时候value就是nil,所以直接跳出循环不输出任何值。
  13. >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
  14. >Exitcode:0
  15. 那么,如果是
  16. fork,vinpairs(tabFiles)do
  17. print(k,v)
  18. end
  19. 则会输出所有:
  20. >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
  21. 3test2
  22. 6test3
  23. 4test1
  24. >Exitcode:0
  25. 现在改变一下表内容,
  26. localtabFiles={
  27. [1]="test1",
  28. [6]="test2",
  29. [4]="test3"
  30. }
  31. fork,vinipairs(tabFiles)do
  32. print(k,v)
  33. end
  34. 现在的输出结果显而易见就是key=1时的value值test1
  35. >lua-e"io.stdout:setvbuf‘no‘""Test.lua"
  36. 1test1
  37. >Exitcode:0
  38. --[示例1.]--
  39. localtt=
  40. {
  41. [1]="test3",
  42. [4]="test4",
  43. [5]="test5"
  44. }
  45. fori,vinpairs(tt)do--输出"test4""test3""test5"
  46. print(tt[i])
  47. end
  48. fori,vinipairs(tt)do--输出"test3"k=2时断开
  49. print(tt[i])
  50. end
  51. --[[示例2.]]--
  52. tbl={"alpha","beta",[3]="uno",["two"]="dos"}
  53. fori,vinipairs(tbl)do--输出前三个
  54. print(tbl[i])
  55. end
  56. fori,vinpairs(tbl)do--全部输出
  57. print(tbl[i])
  58. end

www.cppblog.com/wc250en007/archive/2011/12/16/162203.html