{"id":247343,"date":"2022-07-25T10:22:17","date_gmt":"2022-07-25T02:22:17","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=247343"},"modified":"2022-07-22T10:22:59","modified_gmt":"2022-07-22T02:22:59","slug":"lua-state-iterator","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lua-state-iterator.html","title":{"rendered":"Lua\u6709\u72b6\u6001\u8fed\u4ee3\u5668"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u8fed\u4ee3\u5668\u9700\u8981\u4fdd\u5b58\u591a\u4e2a\u72b6\u6001\u4fe1\u606f\u800c\u4e0d\u662f\u7b80\u5355\u7684\u72b6\u6001\u5e38\u91cf\u548c\u63a7\u5236\u53d8\u91cf\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u95ed\u5305\uff0c\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u5c06\u6240\u6709\u7684\u72b6\u6001\u4fe1\u606f\u5c01\u88c5\u5230 table \u5185\uff0c\u5c06 table \u4f5c\u4e3a\u8fed\u4ee3\u5668\u7684\u72b6\u6001\u5e38\u91cf\uff0c\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u4fe1\u606f\u5b58\u653e\u5728 table \u5185\uff0c\u6240\u4ee5\u8fed\u4ee3\u51fd\u6570\u901a\u5e38\u4e0d\u9700\u8981\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6848\u4f8b<\/strong><\/div>\n

\u6709\u72b6\u6001\u8fed\u4ee3\u5668
\n\u6709\u72b6\u6001\u8fed\u4ee3\u5668\u7684\u4f7f\u7528<\/p>\n

\r\n#!\/usr\/bin\/lua\r\nprint(\"haicoder(www.haicoder.net)\\n\")\r\narray = {\"Hello\", \"HaiCoder\", \"Lua\"}\r\nfunction elementIterator (collection)\r\n   local index = 0\r\n   local count = #collection\r\n   -- \u95ed\u5305\u51fd\u6570\r\n   return function ()\r\n      index = index + 1\r\n      if index <= count\r\n      then\r\n         --  \u8fd4\u56de\u8fed\u4ee3\u5668\u7684\u5f53\u524d\u5143\u7d20\r\n         return collection[index]\r\n      end\r\n   end\r\nend\r\nfor element in elementIterator(array)\r\ndo\r\n   print(string.format(\"Element = %s\", element))\r\nend\r\n<\/pre>\n

\u7a0b\u5e8f\u8fd0\u884c\uff0c\u63a7\u5236\u53f0\u8f93\u51fa\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n

\"\"<\/p>\n

\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u3000elementIterator \u51fd\u6570\u5185\u5b9a\u4e49\u4e86\u53e6\u5916\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u3002\u6b64\u533f\u540d\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u5916\u90e8\u53d8\u91cf index (\u8bd1\u6ce8\uff1a\u6b64\u53d8\u91cf\u5728\u533f\u540d\u51fd\u6570\u4e4b\u5916\uff0celementIterator \u51fd\u6570\u5185)\u3002\u6bcf\u6b21\u5185\u90e8\u7684\u533f\u540d\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u90fd\u4f1a\u5c06 index \u7684\u503c\u589e\u52a0 1\uff0c\u5e76\u7edf\u8ba1\u6570\u8fd4\u56de\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u53c2\u7167\u4e0a\u9762\u7684\u65b9\u6cd5\u4f7f\u7528\u95ed\u5305\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\u51fd\u6570\u3002\u6bcf\u6b21\u6211\u4eec\u4f7f\u7528\u8fed\u4ee3\u5668\u904d\u5386\u96c6\u5408\u65f6\uff0c\u5b83\u90fd\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n

Lua\u6709\u72b6\u6001\u8fed\u4ee3\u5668\u603b\u7ed3<\/strong><\/div>\n

\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u8fed\u4ee3\u5668\u9700\u8981\u4fdd\u5b58\u591a\u4e2a\u72b6\u6001\u4fe1\u606f\u800c\u4e0d\u662f\u7b80\u5355\u7684\u72b6\u6001\u5e38\u91cf\u548c\u63a7\u5236\u53d8\u91cf\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u95ed\u5305\uff0c\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u5c31\u662f\u5c06\u6240\u6709\u7684\u72b6\u6001\u4fe1\u606f\u5c01\u88c5\u5230 table \u5185\uff0c\u5c06 table \u4f5c\u4e3a\u8fed\u4ee3\u5668\u7684\u72b6\u6001\u5e38\u91cf\uff0c\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u4fe1\u606f\u5b58\u653e\u5728 table \u5185\uff0c\u6240\u4ee5\u8fed\u4ee3\u51fd\u6570\u901a\u5e38\u4e0d\u9700\u8981\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6709\u72b6\u6001\u8fed\u4ee3\u5668 \u6709\u72b6\u6001\u8fed\u4ee3\u5668\u7684\u4f7f\u7528 #!\/usr\/bin\/lua print(“haicoder(www.hai […]<\/p>\n","protected":false},"author":1903,"featured_media":247653,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-247343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/247343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=247343"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/247343\/revisions"}],"predecessor-version":[{"id":247655,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/247343\/revisions\/247655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/247653"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=247343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=247343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=247343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}