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

            常用鏈接

            留言簿(7)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            cocos2d-x

            OGRE

            OPenGL

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            標(biāo)準(zhǔn)庫提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代數(shù)組元素的(ipairs),迭代字符串中單詞的 

            (string.gmatch)等等。LUA手冊中對(duì)與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,只能返回?cái)?shù)字0,如果遇到nil則退出。它只能遍歷到表中出現(xiàn)的第一個(gè)不是整數(shù)的key

             


            下面舉個(gè)例子吧!

             

             eg:

            local tabFiles = {

            [3] = "test2",

            [6] = "test3",

            [4] = "test1"

            }

             

            for k, v in ipairs(tabFiles) do

            print(k, v)

            end

             

             

            猜測它的輸出結(jié)果是什么呢?

             

            根據(jù)剛才的分析,它在 ipairs(tabFiles) 遍歷中,當(dāng)key=1時(shí)候value就是nil,所以直接跳出循環(huán)不輸出任何值。

             

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

            >Exit code: 0

             

            那么,如果是

            for k, v in pairs(tabFiles) do

            print(k, v)

            end

            則會(huì)輸出所有 :
            >lua -e "io.stdout:setvbuf 'no'" "Test.lua" 
            3 test2
            6 test3
            4 test1
            >Exit code: 0
            現(xiàn)在改變一下表內(nèi)容,
            local tabFiles = {
            [1] = "test1",
            [6] = "test2",
            [4] = "test3"
            }
            for k, v in ipairs(tabFiles) do
            print(k, v)
            end
            現(xiàn)在的輸出結(jié)果顯而易見就是key=1時(shí)的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時(shí)斷開
                print( tt[i] )
            end





            -- [[示例2.]] --
            tbl 
            = {"alpha""beta", [3= "uno", ["two"= "dos"}

            for i,v in ipairs(tbl) do    --輸出前三個(gè)
                print( tbl[i] )
            end

            for i,v in pairs(tbl) do    --全部輸出
                print( tbl[i] )
            end




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

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

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

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

            謝謝分享  回復(fù)  更多評(píng)論
              
            久久夜色撩人精品国产| 色妞色综合久久夜夜| 久久精品中文字幕久久| 91精品国产综合久久香蕉| 久久国产午夜精品一区二区三区| 亚洲欧美另类日本久久国产真实乱对白 | 久久久久亚洲AV成人网人人网站 | 性高湖久久久久久久久AAAAA| 浪潮AV色综合久久天堂| 国产精品美女久久久免费| 亚洲国产欧美国产综合久久| 国产高潮国产高潮久久久91 | 久久婷婷国产剧情内射白浆| 久久99国产精品久久久| 亚洲欧美日韩久久精品| 岛国搬运www久久| 国产成年无码久久久久毛片| 久久笫一福利免费导航 | 精品久久久久久久无码| 国产免费久久精品99re丫y| 94久久国产乱子伦精品免费 | 亚洲精品白浆高清久久久久久| 久久99精品久久久久久不卡| 国产91色综合久久免费| 亚洲va久久久噜噜噜久久天堂| 久久中文精品无码中文字幕| 日本福利片国产午夜久久| 精品少妇人妻av无码久久| 亚洲精品无码久久久影院相关影片| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久精品9988| 热99re久久国超精品首页| 国产精品久久永久免费| 久久精品亚洲一区二区三区浴池| 亚洲色欲久久久综合网东京热| 久久精品人妻一区二区三区| 久久播电影网| 久久夜色精品国产亚洲av| 亚洲美日韩Av中文字幕无码久久久妻妇 | 无码日韩人妻精品久久蜜桃| 久久久无码精品亚洲日韩按摩|