• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 132  文章 - 51  trackbacks - 0
            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(7)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            cocos2d-x

            OGRE

            OPenGL

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            標準庫提供了集中迭代器,包括迭代文件每行的(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 

            --[示例1.]--
            local tt 
            =
            {
                [
            1= "test3",
                [
            4= "test4",
                [
            5= "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", [3= "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




            posted on 2011-12-16 08:43 風輕云淡 閱讀(20928) 評論(3)  編輯 收藏 引用 所屬分類: LUA

            FeedBack:
            # re: lua 中pairs 和 ipairs區別[未登錄] 2014-03-28 16:31 kyle
            英語不好就別誤人子弟了

            ---------------------------Yours-----------
            pairs可以遍歷表中所有的key,并且除了迭代器本身以及遍歷表本身還可以返回nil;
            但是ipairs則不能返回nil,只能返回數字0,如果遇到nil則退出。它只能遍歷到表中出現的第一個不是整數的key

            --------------------------Mine------------
            pairs遍歷表中全部key,value
            ipairs從下標為1開始遍歷,然后下標累加1,如果某個下標元素不存在就終止遍歷。這就導致如果下標不連續或者不是從1開始的表就會中斷或者遍歷不到元素。  回復  更多評論
              
            # re: lua 中pairs 和 ipairs區別 2015-08-01 13:15 damnimgood
            感謝分享 獲益了  回復  更多評論
              
            # re: lua 中pairs 和 ipairs區別 2015-09-04 22:32 Rming
            @kyle

            謝謝分享  回復  更多評論
              
            久久人人爽人人爽人人片AV不| 亚洲色婷婷综合久久| 久久国产福利免费| 久久午夜无码鲁丝片秋霞| 久久综合久久自在自线精品自| 91精品观看91久久久久久| 三级三级久久三级久久| 国产91久久综合| 久久国产精品无码一区二区三区| 91精品国产91久久| 久久99精品久久久久子伦| 污污内射久久一区二区欧美日韩| 精品久久久久久成人AV| 无码人妻久久一区二区三区蜜桃 | 91久久福利国产成人精品| 亚洲一区精品伊人久久伊人 | 大蕉久久伊人中文字幕| 久久婷婷五月综合色高清| 日韩欧美亚洲综合久久影院Ds| 久久久久夜夜夜精品国产| 久久久久国产精品人妻| 久久亚洲色一区二区三区| 91久久香蕉国产熟女线看| 日韩久久久久久中文人妻| 久久亚洲AV无码精品色午夜| 久久最新免费视频| 66精品综合久久久久久久| 国产一区二区精品久久| …久久精品99久久香蕉国产| 久久午夜羞羞影院免费观看| 精品国产乱码久久久久软件| 中文字幕久久精品| 亚洲精品成人久久久| 理论片午午伦夜理片久久| 久久精品女人天堂AV麻| 久久久久亚洲AV成人网人人软件 | 久久精品这里热有精品| 国产产无码乱码精品久久鸭 | MM131亚洲国产美女久久| 国产精品一区二区久久不卡| 国产91色综合久久免费|