婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Lua中ipair和pair的區(qū)別

Lua中ipair和pair的區(qū)別

熱門(mén)標(biāo)簽:武漢百應(yīng)人工智能電銷(xiāo)機(jī)器人 400手機(jī)電話(huà)免費(fèi)辦理 揚(yáng)州電銷(xiāo)外呼系統(tǒng)軟件 上海企業(yè)外呼系統(tǒng)排名 百度地圖標(biāo)注位置網(wǎng)站 如何利用高德地圖標(biāo)注家 開(kāi)通400電話(huà)申請(qǐng)流程 電腦外呼系統(tǒng)輻射大嗎 智能語(yǔ)音電銷(xiāo)的機(jī)器人

先看看官方手冊(cè)的說(shuō)明吧:

復(fù)制代碼 代碼如下:

pairs (t)If t has a metamethod __pairs, calls it with t as argument and returns the first three results from the call.
Otherwise, 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 (t)If t has a metamethod __ipairs, calls it with t as argument and returns the first three results from the call.
Otherwise, 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.

原來(lái),pairs會(huì)遍歷table的所有鍵值對(duì)。如果你看過(guò)耗子叔的Lua簡(jiǎn)明教程,你知道table就是鍵值對(duì)的數(shù)據(jù)結(jié)構(gòu)。

而ipairs就是固定地從key值1開(kāi)始,下次key累加1進(jìn)行遍歷,如果key對(duì)應(yīng)的value不存在,就停止遍歷。順便說(shuō)下,記憶也很簡(jiǎn)單,帶i的就是根據(jù)integer key值從1開(kāi)始遍歷的。

請(qǐng)看個(gè)例子。

復(fù)制代碼 代碼如下:

tb = {"oh", [3] = "god", "my", [5] = "hello", [6] = "world"}

for k,v in ipairs(tb) do
     print(k, v)
end


輸出結(jié)果就是:
復(fù)制代碼 代碼如下:

1       oh
2       my
3       god

因?yàn)閠b不存在tb[4],所以遍歷到此為止了。
復(fù)制代碼 代碼如下:

for k,v in pairs(tb) do
     print(k, v)
end

輸出結(jié)果:
復(fù)制代碼 代碼如下:

1       oh
2       my
3       god
6       world
5       hello

我們都能猜到,將輸出所有的內(nèi)容。然而你發(fā)現(xiàn)輸出的順序跟你tb中的順序不同。
如果我們要按順序輸出怎么辦?辦法之一是:
復(fù)制代碼 代碼如下:

for i = 1, #tb do
     if tb[i] then
          print(tb[i])
     else
end

當(dāng)然,僅僅是個(gè)數(shù)組的話(huà),ipairs也沒(méi)問(wèn)題。

以上(為什么不少回答會(huì)以「以上」收尾?,這里就是結(jié)束的意思吧)

標(biāo)簽:新余 江西 黑龍江 延邊 嘉峪關(guān) 武漢 宜賓 張掖

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lua中ipair和pair的區(qū)別》,本文關(guān)鍵詞  Lua,中,ipair,和,pair,的,區(qū)別,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Lua中ipair和pair的區(qū)別》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Lua中ipair和pair的區(qū)別的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 天全县| 什邡市| 耿马| 九龙城区| 磐安县| 古浪县| 科尔| 绩溪县| 米林县| 榆社县| 阳信县| 内丘县| 蒙阴县| 金川县| 凉城县| 东乌珠穆沁旗| 运城市| 遵化市| 东辽县| 措美县| 隆尧县| 贵南县| 宜宾县| 晋州市| 民权县| 余庆县| 深州市| 准格尔旗| 钟祥市| 钟祥市| 广元市| 唐山市| 临沭县| 尉氏县| 乌审旗| 弥渡县| 新河县| 通州市| 济源市| 定结县| 武宁县|