{"id":525,"date":"2022-05-14T17:10:52","date_gmt":"2022-05-14T09:10:52","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=525"},"modified":"2022-09-06T17:11:16","modified_gmt":"2022-09-06T09:11:16","slug":"lua%e4%b8%adpairs%e5%92%8cipairs%e9%83%bd%e5%81%9a%e4%ba%86%e4%bb%80%e4%b9%88%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=525","title":{"rendered":"lua\u4e2dpairs\u548cipairs\u90fd\u505a\u4e86\u4ec0\u4e48\u64cd\u4f5c"},"content":{"rendered":"<h1>1. \u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50<\/h1>\n<h2>\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u51fa ipairs\u53ea\u8f93\u51fa\u4e86\u6570\u7ec4\u90e8\u5206,nil\u622a\u65ad\u4e4b\u540e\u4e5f\u4e0d\u4f1a\u8f93\u51fa<\/h2>\n<h2>pairs \u4f1a\u8f93\u51fa\u5168\u90e8\u5185\u5bb9<\/h2>\n<pre><code class=\"line-numbers\">a = {1, 2, nil, 4, b = 1}\n\nfor k, v in ipairs(a) do\n    print(\"ipairs\", k, v);\nend\nfor k, v in pairs(a) do\n    print(\"pairs\", k, v);\nend\n\n\/\/ \u8f93\u51fa\nipairs  1       1\nipairs  2       2\npairs   1       1\npairs   2       2\npairs   4       4\npairs   b       1\n\n\n<\/code><\/pre>\n<h1>ipairs\u662f\u600e\u4e48\u53d6\u503c\u7684<\/h1>\n<h2>\u53ef\u4ee5\u770b\u51fa \u6bcf\u6b21\u53d6\u503c\u662fi++ \u7684\u53d6\u503c\u6cd5<\/h2>\n<pre><code class=\"line-numbers\">static int ipairsaux(gafq_State *L)\n{\n    int i = gafqL_checkint(L, 2);\n    gafqL_checktype(L, 1, GAFQ_TTABLE);\n    i++; \/\/ \u53ef\u4ee5\u770b\u51fa \u6bcf\u6b21\u53d6\u503c\u662fi++ \u7684\u53d6\u503c\u6cd5\n    gafq_pushinteger(L, i);\n    gafq_rawgeti(L, 1, i);\n    return (gafq_isnil(L, -1)) ? 0 : 2;\n}\n<\/code><\/pre>\n<h1>pairs\u662f\u600e\u4e48\u53d6\u503c\u7684<\/h1>\n<h2>\u53ef\u4ee5\u770b\u51fa pairs\u662f\u901a\u8fc7 \u6808\u9876\u6765\u53d6\u503c\u7684\u5728gafqH_next\u4e2d\u53ef\u4ee5\u770b\u51fa \u4f1a\u5148\u53bbarray\u90e8\u5206 \u5728\u53d6hash\u90e8\u5206<\/h2>\n<pre><code class=\"line-numbers\">static int gafqBase_next(gafq_State *L)\n{\n    gafqL_checktype(L, 1, GAFQ_TTABLE);\n    gafq_settop(L, 2);\n    if (gafq_next(L, 1))\n        return 2;\n    else\n    {\n        gafq_pushnil(L);\n        return 1;\n    }\n}\n\nGAFQ_API int gafq_next(gafq_State *L, int idx)\n{\n    StkId t;\n    int more;\n    gafq_lock(L);\n    t = idx_to_Tvalue(L, idx);\n    api_check(L, ttistable(t));\n    more = gafqH_next(L, hvalue(t), L-&gt;top - 1);\n    if (more)\n    {\n        api_incr_top(L);\n    }\n    else              \n        L-&gt;top -= 1;\n    gafq_unlock(L);\n    return more;\n}\n\nint gafqH_next(gafq_State *L, Table *t, StkId key)\n{\n    int i = findindex(L, t, key);\n    for (i++; i &lt; t-&gt;sizearray; i++)\n    {  \n        if (!ttisnil(&amp;t-&gt;array[i]))\n        {\n            setnvalue(key, cast_num(i + 1));\n            setobj2s(L, key + 1, &amp;t-&gt;array[i]);\n            return 1;\n        }\n    }\n    for (i -= t-&gt;sizearray; i &lt; sizenode(t); i++)\n    { \n        if (!ttisnil(gval(gnode(t, i))))\n        { \n            setobj2s(L, key, key2tval(gnode(t, i)));\n            setobj2s(L, key + 1, gval(gnode(t, i)));\n            return 1;\n        }\n    }\n    return 0;\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e00\u4e9b\u7b80\u5355\u7684\u4f8b\u5b50 \u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u51fa ipairs\u53ea\u8f93\u51fa\u4e86\u6570\u7ec4\u90e8\u5206,nil\u622a\u65ad\u4e4b\u540e\u4e5f\u4e0d\u4f1a\u8f93\u51fa pairs \u4f1a\u8f93<a href=\"https:\/\/www.ccagml.com\/?p=525\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">lua\u4e2dpairs\u548cipairs\u90fd\u505a\u4e86\u4ec0\u4e48\u64cd\u4f5c<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/525"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=525"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":526,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/525\/revisions\/526"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}