1. 程式人生 > >lua中 pairs 和 ipairs區別 和用法

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 = {

[3] = "test2",

[6] = "test3",

[4] = "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: 0

那麼,如果是

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

 >lua -e "io.stdout:setvbuf 'no'" "Test.lua" 

1 test1

>Exit code: 0 

[cpp] view plain copy  print?
  1. 下面舉個例子吧!  
  2.  eg:  
  3. local tabFiles = {  
  4. [3] = "test2",  
  5. [6] = "test3",  
  6. [4] = "test1"
  7. }  
  8. for k, v in ipairs(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. >Exit code: 0  
  15. 那麼,如果是  
  16. for k, v in pairs(tabFiles) do
  17. print(k, v)  
  18. end  
  19. 則會輸出所有 :  
  20. >lua -e "io.stdout:setvbuf 'no'""Test.lua"
  21. 3 test2  
  22. 6 test3  
  23. 4 test1  
  24. >Exit code: 0  
  25. 現在改變一下表內容,  
  26. local tabFiles = {  
  27. [1] = "test1",  
  28. [6] = "test2",  
  29. [4] = "test3"
  30. }  
  31. for k, v in ipairs(tabFiles) do
  32. print(k, v)  
  33. end  
  34. 現在的輸出結果顯而易見就是key=1時的value值test1  
  35.  >lua -e "io.stdout:setvbuf 'no'""Test.lua"
  36. 1 test1  
  37. >Exit code: 0   
  38. --[示例1.]--  
  39. local tt =  
  40. {  
  41.     [1] = "test3",  
  42.     [4] = "test4",  
  43.     [5] = "test5"
  44. }  
  45. for i,v in pairs(tt) do        -- 輸出 "test4""test3""test5"
  46.     print( tt[i] )  
  47. end  
  48. for i,v in ipairs(tt) do    -- 輸出 "test3" k=2時斷開  
  49.     print( tt[i] )  
  50. end  
  51. -- [[示例2.]] --  
  52. tbl = {"alpha""beta", [3] = "uno", ["two"] = "dos"}  
  53. for i,v in ipairs(tbl) do    --輸出前三個  
  54.     print( tbl[i] )  
  55. end  
  56. for i,v in pairs(tbl) do    --全部輸出  
  57.     print( tbl[i] )  
  58. end