• <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
            <2010年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            常用鏈接

            留言簿(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免费看| 久久国产精品波多野结衣AV| 久久久国产打桩机| 天天躁日日躁狠狠久久| 26uuu久久五月天| 中文字幕无码免费久久| 午夜不卡888久久| 久久99精品久久久大学生| 91久久精品无码一区二区毛片| 久久久久久久久久免免费精品| 亚洲伊人久久成综合人影院| 国产亚洲美女精品久久久久狼| 日韩va亚洲va欧美va久久| 久久精品国产一区| 日本强好片久久久久久AAA| 久久精品国产精品亜洲毛片| 精品久久久久香蕉网| 综合网日日天干夜夜久久 | 久久综合国产乱子伦精品免费| A级毛片无码久久精品免费| 久久九九精品99国产精品| 亚洲精品久久久www| 国内精品久久久久久久涩爱| 久久精品九九亚洲精品| 久久天天躁狠狠躁夜夜avapp| 中文精品久久久久人妻| 久久久久久久综合狠狠综合| 国产精品伊人久久伊人电影| 亚洲精品国产成人99久久| 99久久99久久精品免费看蜜桃| 亚洲国产精品无码成人片久久| 久久人人超碰精品CAOPOREN| 久久播电影网| 久久久国产精品| 亚洲国产成人久久综合一区77 | 精品无码久久久久久久动漫| 久久久久久免费一区二区三区| 国产精品久久毛片完整版| 久久青青草原精品影院| 国产 亚洲 欧美 另类 久久| 久久天天躁狠狠躁夜夜2020老熟妇|