• <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年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(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 風輕云淡 閱讀(20890) 評論(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

            謝謝分享  回復  更多評論
              
            久久久久久国产精品美女| 久久精品免费一区二区三区| 91精品国产91热久久久久福利 | 久久精品国产99国产精偷| 亚洲欧美久久久久9999| 精品少妇人妻av无码久久| 亚洲国产精品无码久久九九| 午夜精品久久久内射近拍高清| 狠狠色噜噜色狠狠狠综合久久| 精品久久久久久久| 国产美女久久久| 亚洲婷婷国产精品电影人久久| 天天躁日日躁狠狠久久| 99久久婷婷国产一区二区| 久久久午夜精品福利内容| 人妻丰满?V无码久久不卡| 99久久免费国产精精品| 国产精品美女久久久m| 久久99九九国产免费看小说| 久久91精品国产91| 久久国产精品99精品国产987| 精品久久久无码21p发布| 久久久久久久综合狠狠综合| 99久久99久久精品免费看蜜桃| 久久久久久久波多野结衣高潮| 欧美精品一区二区精品久久| 久久香蕉综合色一综合色88| 亚洲日本va中文字幕久久| 久久久久亚洲爆乳少妇无| 久久人人爽人人人人爽AV| 久久夜色撩人精品国产| 久久精品国产半推半就| 久久国产精品99精品国产987| 麻豆一区二区99久久久久| 久久人人爽人人爽人人AV| 国内精品伊人久久久久妇| 久久无码人妻精品一区二区三区 | 一本久久a久久精品综合夜夜| 奇米影视7777久久精品| 午夜精品久久影院蜜桃| 伊人久久大香线蕉精品不卡|