{"id":138,"date":"2025-10-18T15:56:14","date_gmt":"2025-10-18T07:56:14","guid":{"rendered":"http:\/\/47.99.178.170\/?p=138"},"modified":"2025-10-18T17:30:21","modified_gmt":"2025-10-18T09:30:21","slug":"%e3%80%90%e7%bc%96%e7%a8%8b%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a0%e3%80%91lua%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=138","title":{"rendered":"\u3010\u7f16\u7a0b\u8bed\u8a00\u5b66\u4e60\u3011Lua\u7a0b\u5e8f\u8bbe\u8ba1(1) \u57fa\u7840\u8bed\u6cd5"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u53c2\u8003\u6587\u6863<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u300aLua\u7a0b\u5e8f\u8bbe\u8ba1\uff08\u7b2c\u4e8c\u7248\uff09\u300b &#8211;\u7535\u5b50\u5de5\u4e1a\u51fa\u7248\u793e [\u5df4\u897f]Roberto Ierusalimschy \u8457 \u5468\u60df\u8fea \u8bd1<\/li>\n\n\n\n<li>\u83dc\u9e1f\u6559\u7a0b(RUNOOB.COM)<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u7c7b\u578b\u4e0e\u503c<\/h2>\n\n\n\n<p><code>Lua<\/code>\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u5728\u8bed\u8a00\u4e2d\u6ca1\u6709\u7c7b\u578b\u5b9a\u4e49\u7684\u8bed\u6cd5\uff0c\u4efb\u4f55\u53d8\u91cf\u90fd\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u7c7b\u578b\u7684\u503c\uff1b<code>Lua<\/code>\u4e2d\u67098\u4e2d\u57fa\u7840\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570<code>type<\/code>\u8fd4\u56de\u4e00\u4e2a\u503c\u7684\u7c7b\u578b\u540d\u79f0<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>nil<\/code>\u7a7a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>print(type(nil))                --->nil\n\n-->\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u6ca1\u6709\u521d\u59cb\u5316\uff0c\u5b83\u7684\u7c7b\u578b\u4e3a`nil`\nlocal a\nprint(type(a))           --->nil<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><code>boolean<\/code>\u5e03\u5c14<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>print(type(true))                --->boolean<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><code>number<\/code>\u6570\u5b57<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>print(type(10))                  --->number<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><code>string<\/code>\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>print(type(\"Hello world\"))       --->string<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><code>userdata<\/code>\u81ea\u5b9a\u4e49\u7c7b\u578b<\/li>\n\n\n\n<li><code>function<\/code>\u51fd\u6570<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>print(type(print))        --->function<\/code><\/pre>\n\n\n\n<ol start=\"7\" class=\"wp-block-list\">\n<li><code>thread<\/code>\u7ebf\u7a0b<\/li>\n\n\n\n<li><code>table<\/code>\u8868<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>local a ={}\nprint(type(a))            --->table\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 nil\uff08\u7a7a\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>nil<\/code>\u7c7b\u578b\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7c7b\u578b\uff0c\u4ec5\u6709\u4e00\u4e2a\u503c<code>nil<\/code>\uff1b\u5176\u4e3b\u8981\u529f\u80fd\u5c31\u662f\u7528\u4e8e\u533a\u5206\u5176\u4ed6\u4efb\u4f55\u503c\uff0c\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u5728\u7b2c\u4e00\u6b21\u8d4b\u503c\u524d\u7684\u9ed8\u8ba4\u503c\u5c31\u662f<code>nil<\/code>\uff0c\u5c06<code>nil<\/code>\u8d4b\u4e88\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u7b49\u540c\u4e8e\u5220\u9664\u5b83\uff0c<code>Lua<\/code>\u5c06<code>nil<\/code>\u7528\u4e8e\u8868\u8ff0\u4e00\u79cd\u65e0\u6548\u503c\u7684\u60c5\u51b5<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 Boolean\uff08\u5e03\u5c14\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>boolean<\/code>\u7c7b\u578b\u6709\u4e24\u4e2a\u503c\u53ef\u4ee5\u9009\u62e9<code>false<\/code>\u548c<code>true<\/code>\uff1b<code>Lua<\/code>\u5c06<code>false<\/code>\u548c<code>nil<\/code>\u89c6\u4e3a\u201c\u5047\u201d\uff0c\u5176\u4f59\u4e4b\u5916\u7684\u5176\u4ed6\u503c\u89c6\u4e3a\u201c\u771f\u201d<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 number\uff08\u6570\u5b57\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u4e2d\u5bf9\u4e8e\u6574\u6570\u548c\u6d6e\u70b9\u6570\u4e0d\u8fdb\u884c\u533a\u5206\uff0c\u5168\u90e8\u4f7f\u7528<code>number<\/code>\u7c7b\u578b\u8868\u793a<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1.4 string\uff08\u5b57\u7b26\u4e32\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u4e2d\u7684\u5b57\u7b26\u4e32\u9700\u8981\u4ee5\u4e00\u5bf9\u5339\u914d\u7684\u5355\u5f15\u53f7\u6216\u8005\u53cc\u5f15\u53f7\u6765\u754c\u5b9a<\/li>\n\n\n\n<li>\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u7c7b\u4f3c\u4e8e<code>C<\/code>\u8bed\u8a00\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26<\/li>\n\n\n\n<li>\u4f7f\u7528\u5b57\u6bcd\u8fdb\u884c\u8f6c\u4e49\uff0c\u5982<code>\\n \u6362\u884c<\/code>\u3001 <code>\\r \u56de\u8f66<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528<code>ASCII<\/code>\u7801\u6765\u6307\u5b9a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u5982<code>\\97 a<\/code>\u3001<code>\\10 \\n<\/code><\/li>\n\n\n\n<li>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u8f6c\u4e49\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u5bf9\u5339\u914d\u7684\u53cc\u65b9\u62ec\u53f7\u754c\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32<code>Lua<\/code>\u4e0d\u4f1a\u89e3\u91ca\u5176\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26\uff0c\u8fd9\u79cd\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5ef6\u4f38\u591a\u884c <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>a = \"\\97\\97\"\nb = &#91;&#91;\\97\\97]]\n\nprint(a)          --->aa\nprint(b)          --->\\97\\97<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26<code>..<\/code>\uff0c\u5f53\u76f4\u63a5\u5728\u4e00\u4e2a\u6570\u5b57\u540e\u9762\u8f93\u5165\u5b83\u65f6\u5fc5\u987b\u8981\u7528\u4e00\u4e2a\u7a7a\u683c\u6765\u5206\u9694\u5b83\u4eec\uff0c\u4e0d\u7136<code>Lua<\/code>\u4f1a\u5c06\u7b2c\u4e00\u4e2a\u70b9\u7406\u89e3\u4e3a\u5c0f\u6570\u70b9\uff0c\u6216\u8005\u76f4\u63a5\u62a5\u9519<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>a = \"abc\"\nb = 123\n\nrint(a..123)         --->abc123\nprint(123 ..456)      --->123456<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5b57\u7b26\u4e32\u524d\u653e\u7f6e\u64cd\u4f5c\u7b26<code>#<\/code>\u53ef\u4ee5\u83b7\u5f97\u8be5\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>a = \"abc\"\n\nprint(#a)          --->3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.5 table(\u8868)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code> \u4e2d\u7684\u8868<code>table<\/code>\u5176\u5b9e\u662f\u4e00\u4e2a&#8221;\u5173\u8054\u6570\u7ec4&#8221;<code>associative arrays<\/code>\uff0c\u6570\u7ec4\u7684\u7d22\u5f15\u53ef\u4ee5\u662f\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u6216\u8868\u7c7b\u578b\u3002\u5728 <code>Lua<\/code> \u91cc\uff0c<code>table<\/code> \u7684\u521b\u5efa\u662f\u901a\u8fc7&#8221;\u6784\u9020\u8868\u8fbe\u5f0f&#8221;\u6765\u5b8c\u6210\uff0c\u6700\u7b80\u5355\u6784\u9020\u8868\u8fbe\u5f0f\u662f<code>{}<\/code>\uff0c\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u8868\u3002<\/li>\n\n\n\n<li><code>Lua table<\/code>\u662f\u4e0d\u56fa\u5b9a\u5927\u5c0f\u7684\uff0c\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u8fdb\u884c\u6269\u5145<\/li>\n\n\n\n<li><code>Lua table<\/code> \u4f7f\u7528\u5173\u8054\u578b\u6570\u7ec4\uff0c\u4f60\u53ef\u4ee5\u7528\u4efb\u610f\u7c7b\u578b\u7684\u503c\u6765\u4f5c\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u4f46\u8fd9\u4e2a\u503c\u4e0d\u80fd\u662f <code>nil<\/code><\/li>\n\n\n\n<li><code>Lua<\/code>\u4e5f\u662f\u901a\u8fc7table\u6765\u89e3\u51b3\u6a21\u5757<code>module<\/code>\u3001\u5305<code>package<\/code>\u548c\u5bf9\u8c61<code>Object<\/code>\u7684\u7ba1\u7406<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.5.1 table\u7684\u7279\u6027<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e00\u4e2a<code>table<\/code>\u4e2d\u5305\u542b\u8bb8\u591a\u6210\u5458\uff0c\u6bcf\u4e2a\u6761\u76ee\u5305\u542b\u4e00\u4e2a\u7d22\u5f15\u548c\u4e00\u4e2a\u5143\u7d20\uff0c\u7d22\u5f15\u548c\u5143\u7d20\u4e3a\u4e00\u7ec4\u952e\u503c\u5bf9<\/li>\n\n\n\n<li><code>table<\/code>\u7684\u7d22\u5f15\u521d\u59cb\u503c\u9ed8\u8ba4\u4e3a1\uff0c\u4e14\u7d22\u5f15\u4e0d\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570<\/li>\n\n\n\n<li><code>table<\/code>\u7684\u957f\u5ea6\u53ef\u4ee5\u52a8\u6001\u589e\u957f<\/li>\n\n\n\n<li>\u4e0d\u540c\u7684\u53d8\u91cf\u53ef\u4ee5\u6301\u6709\u76f8\u540c\u7684<code>table<\/code>\uff0c\u4e00\u4e2a\u6301\u6709<code>table<\/code>\u7684\u53d8\u91cf\u4e0e<code>table<\/code>\u81ea\u8eab\u4e4b\u95f4\u6ca1\u6709\u56fa\u5b9a\u7684\u5173\u8054\u6027<\/li>\n\n\n\n<li>\u5f53\u6ca1\u6709\u4efb\u4f55\u5bf9<code>table<\/code>\u7684\u5f15\u7528\u65f6\uff0c<code>Lua<\/code>\u7684\u5783\u573e\u56de\u6536\u5668\u4f1a\u5220\u9664\u8be5<code>table<\/code>\u5e76\u590d\u7528\u5b83\u7684\u5185\u5b58<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.5.2 table\u521d\u59cb\u5316\u4e0e\u8d4b\u503c<\/h4>\n\n\n\n<p>\u5728\u5b9a\u4e49\u65f6\u521d\u59cb\u5316<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6700\u7b80\u5355\u7684\u6784\u9020\u5f0f\u5c31\u662f\u4e00\u4e2a\u7a7a\u6784\u9020\u5f0f\u5373<code>{}<\/code>\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u7a7a<code>table<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>table_tese = {}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u5f0f\u5728\u521d\u59cb\u5316<code>table<\/code>\u65f6\u8fdb\u884c\u8d4b\u503c<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>local local_A = \"local_A\"\nlocal num_3 = 3\n\ntable_test1 = {\n    --\u98ce\u683c1.\u521d\u59cb\u5316\u65f6\u4f7f\u7528\u201c=\u201d\u8d4b\u503c--\n    style_a_1 = \"a\",\n    style_a_2 = 1,\n    style_a_3 = \"2\",\n    style_a_4 = local_A,\n\n    --\u98ce\u683c2.\u4f7f\u7528\u5c06\u7d22\u5f15\u503c\u9632\u5728\u65b9\u62ec\u53f7\u4e2d--\n    &#91;1] = 1,\n    &#91;2] = \"a\",\n    &#91;num_3] = local_A,\n\n    --\u98ce\u683c3.Lua\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u901a\u7528\u7684\u98ce\u683c\uff0c\u8fd9\u79cd\u683c\u5f0f\u5141\u8bb8\u4f7f\u7528\u8d1f\u6570\u6216\u8fd0\u7b97\u7b26\u4f5c\u4e3a\u7d22\u5f15--\n    &#91;\"-1\"] = \"2\",\n    &#91;\"+\"] = \"3\"\n}\n\n--\u98ce\u683c4.\u4ec5\u586b\u5145\u5143\u7d20\u503c\uff0c\u7d22\u5f15\u503c\u75311\u5f00\u59cb\u81ea\u52a8\u4f9d\u6b21\u9012\u589e--\ntable_test2 = { \"a\", \"b\", \"c\", \"d\" }\n\n--\u98ce\u683c5.\u4ec5\u586b\u5145\u5143\u7d20\u503c\uff0c\u5e76\u6307\u5b9a\u521d\u59cb\u7d22\u5f15\u503c\uff0c\u7d22\u5f15\u503c\u7531\u521d\u59cb\u503c\u5f00\u59cb\u81ea\u52a8\u4f9d\u6b21\u9012\u589e--\ntable_test3 = { &#91;0] = \"a\", \"b\", \"c\", \"d\" }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u521b\u5efa\u540e\u4fee\u6539<code>table<\/code>\u4e2d\u7684\u67d0\u4e9b\u5b57\u6bb5<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>--\u98ce\u683c1. \u4f7f\u7528\u65b9\u62ec\u53f7--\ntable_test&#91;\"a\"] = 3         --->\u5c06\u7d22\u5f15a\u5bf9\u5e94\u7684\u503c\u66f4\u6539\u4e3a3\ntable_test&#91;1] = \"c\"         --->\u5c06\u7d22\u5f151\u5bf9\u5e94\u7684\u503c\u66f4\u6539\u4e3ac\ntable_test&#91;2] = \"d\"         --->\u65b0\u589e\u7d22\u5f152\u5e76\u5c06\u5143\u7d20\u7684\u503c\u8d4b\u503c\u4e3ac\n\n--\u98ce\u683c2.\u4f7f\u7528\u5728table\u53d8\u91cf\u540e\u4f7f\u7528\".\"\u8fde\u63a5\u7d22\u5f15\u503c,\u4f46\u7d22\u5f15\u503c\u4e0d\u53ef\u4ee5\u4e3a\u6570\u7ec4--\ntable_test.a = 3\n\n--\u5220\u9664\u5143\u7d20,\u5c06\u7d22\u5f15\u5bf9\u5e94\u7684\u503c\u7f6e\u4e3anil--\ntable_test.a = nil\ntable_test&#91;1] = nil <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.5.3 table\u904d\u5386<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pairs<\/code>\u53ef\u4ee5\u904d\u5386\u6240\u6709\u6210\u5458<\/li>\n\n\n\n<li><code>Luajit<\/code>\u4e2d\u4f7f\u7528<code>for key, value in pairs(table) do<\/code>\u8fd9\u6837\u7684\u904d\u5386\u987a\u5e8f\u5e76\u975e\u662f\u4ee3\u7801\u4e2d<code>table<\/code>\u4e2d\u7684\u6392\u5217\u987a\u5e8f\uff0c\u800c\u662f\u6839\u636e<code>table<\/code>\u4e2d<code>key<\/code>\u7684<code>hash<\/code>\u503c\u7684\u6392\u5217\u987a\u5e8f\u6765\u904d\u5386\uff0c<code>key<\/code>\u7684<code>hash<\/code>\u662f\u4ee5\u65f6\u95f4\u6233\u76f8\u5173\u7684\u968f\u673a\u503c\u4f5c\u4e3a\u79cd\u5b50\u751f\u6210\u7684\uff0c\u5bfc\u81f4\u6700\u7ec8\u6253\u5370\u7684\u503c\u7684\u987a\u5e8f\u662f\u968f\u673a\u503c<\/li>\n\n\n\n<li><code>ipairs<\/code>\u53ea\u904d\u5386<code>1<\/code>\uff0c<code>2<\/code>\uff0c\u6570\u503c\u578b\u4e0b\u6807\uff0c\u4e0b\u4e00\u4e2a<code>3<\/code>\u4e0d\u8fde\u7eed\u5c31\u4e2d\u65ad\u4e86<\/li>\n\n\n\n<li>\u7531\u4e8e<code>#tbtest<\/code>\u83b7\u53d6\u7684\u503c\u4e3a<code>4<\/code>\uff0c\u6545\u4e0b\u68073\u7684\u503c\u4e3a<code>nil<\/code> <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>table_1 = {\n    a = \"a\",\n    &#91;1] = \"1\",\n    b = \"b\", \n    c = \"c\",\n    &#91;2] = \"2\",\n    &#91;4] = \"4\"\n}\n\nfor key, value in pairs(table_1) do      \n    print(tostring(key)..\":\"..tostring(value))\nend\n\u7ed3\u679c\uff1a\n1:1\n2:2\n4:4\na:a\nb:b\nc:c\n\nfor key, value in ipairs(table_1) do      \n    print(tostring(key)..\":\"..tostring(value))\nend\n\u7ed3\u679c\uff1a\n1:1\n2:2\n\nfor i=1, #table_1 do \n    print(tostring(i)..\":\"..tostring(table_1&#91;i]))\nend\n\u7ed3\u679c\uff1a\n1:1\n2:2\n3:nil\n4:4\n\nlocal table_1 ={\n    {name = \"a\", value = \"1\"},\n    {name = \"c\", value = \"3\"},\n    {name = \"d\", value = \"4\"},\n    {name = \"b\", value = \"2\"},\n}\n\ni = 0\nfor i, k in pairs(table_1) do    \n    for key, value in pairs(k) do    \n        print(tostring(key)..\":\"..tostring(value))\n    end\n    print(\"\\n\")\nend\n\u7ed3\u679c\uff1a\nname:a\nvalue:1\n\nname:c\nvalue:3\n\nname:d\nvalue:4\n\nname:b\nvalue:2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u8bed\u53e5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u652f\u6301\u7684\u5e38\u89c4\u8bed\u53e5\u57fa\u672c\u4e0a\u548c<code>C<\/code>\u8bed\u8a00\u4e2d\u7684\u5dee\u4e0d\u591a\uff0c\u8fd9\u4e9b\u8bed\u53e5\u5305\u62ec\u8d4b\u503c\u3001\u63a7\u5236\u7ed3\u6784\u3001\u8fc7\u7a0b\u8c03\u7528\u7b49\u3002\u53e6\u5916<code>Lua<\/code>\u8fd8\u652f\u6301\u4e00\u4e9b\u4e0d\u592a\u5e38\u89c1\u7684\u8bed\u53e5\uff0c\u6bd4\u5982\u591a\u91cd\u8d4b\u503c\u548c\u5c40\u90e8\u53d8\u91cf\u58f0\u660e<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u591a\u91cd\u8d4b\u503c\u8bed\u53e5<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u5141\u8bb8\u591a\u91cd\u8d4b\u503c\uff0c\u4e5f\u5c31\u662f\u4e00\u6b21\u5c06\u591a\u4e2a\u503c\u8d4b\u7ed9\u591a\u4e2a\u53d8\u91cf\uff0c\u6bcf\u4e2a\u503c\u548c\u53d8\u91cf\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694<\/li>\n\n\n\n<li>\u5982\u679c\u7b49\u53f7\u4e24\u4fa7\u7684\u503c\u6570\u91cf\u4e0d\u540c\u7684\u8bdd\uff0c\u591a\u4f59\u7684\u53d8\u91cf\u4f1a\u88ab\u8d4b\u503c\u4e3a<code>nil<\/code>,\u5982\u679c\u503c\u7684\u4e2a\u6570\u66f4\u591a\u7684\u8bdd\uff0c\u591a\u4f59\u7684\u503c\u4f1a\u88ab\u4e22\u5f03<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>x, y = 10, 10*10\n\nprint(x.. \"\uff1a\" ..y)        --->10:100<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u5c40\u90e8\u53d8\u91cf\u548c\u5757<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u76f8\u5bf9\u4e8e\u5168\u5c40\u53d8\u91cf\uff0c<code>Lua<\/code>\u8fd8\u63d0\u4f9b\u4e86\u5c40\u90e8\u53d8\u91cf\uff0c\u901a\u8fc7<code>local<\/code>\u8bed\u53e5\u6765\u521b\u5efa\u5c40\u90e8\u53d8\u91cf<\/li>\n\n\n\n<li>\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ec5\u9650\u4e8e\u58f0\u660e\u5b83\u4eec\u7684\u90a3\u4e2a\u5757\n<ul class=\"wp-block-list\">\n<li>\u4e00\u4e2a\u5757\u662f\u4e00\u4e2a\u63a7\u5236\u7ed3\u6784\u7684\u6267\u884c\u4f53\u3001\u6216\u8005\u662f\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\u4f53\u6216\u662f\u4e00\u4e2a\u7a0b\u5e8f\u5757<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5c40\u90e8\u53d8\u91cf\u901a\u5e38\u4f1a\u968f\u7740\u5176\u4f5c\u7528\u57df\u7684\u7ed3\u675f\u800c\u6d88\u5931\uff0c\u76f8\u5bf9\u5e94\u7684\u5185\u5b58\u8d44\u6e90\u7531\u5783\u573e\u56de\u6536\u673a\u5236\u8fdb\u884c\u56de\u6536<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u63a7\u5236\u7ed3\u6784<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63a7\u5236\u7ed3\u6784\u4e2d\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u503c\uff0c<code>Lua<\/code>\u5c06\u6240\u6709\u4e0d\u662f<code>false<\/code>\u548c<code>nil<\/code>\u7684\u503c\u89c6\u4e3a\u771f<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 if then else<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5224\u65ad\u6761\u4ef6\u653e\u7f6e\u5728<code>if<\/code>\u548c<code>then<\/code>\u4e4b\u95f4\uff0c\u51fd\u6570\u4f53\u5728<code>then<\/code>\u4e4b\u540e\uff0c\u6574\u4e2a\u5224\u65ad\u8bed\u53e5\u7a0b\u5e8f\u5757\u4ee5<code>end<\/code>\u7ed3\u675f<\/li>\n\n\n\n<li>\u591a\u5c42\u5d4c\u5957\u7ed3\u6784\u4f7f\u7528<code>else if<\/code>\u8bed\u53e5<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  if a &gt; b then\n      print(\"a &gt; b\")\n  elseif a == b then\n      print(\"a = b\")\n  else\n      print(\"a &lt; b\")\n  end<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 while\u5faa\u73af<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5148\u5224\u65ad<code>while<\/code>\u7684\u6761\u4ef6\u662f\u5426\u4e3a\u771f\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u5047\u90a3\u4e48\u7ed3\u675f\u5faa\u73af\uff0c\u4e0d\u7136\u6267\u884c\u5faa\u73af\u4f53\u5e76\u91cd\u590d\u8fd9\u4e00\u8fc7\u7a0b<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  local i = 1\n  while a&#91;i] do\n      print(a&#91;i])\n      i = i + 1\n  end<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 repeat\u5faa\u73af<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u89c6\u4e3a<code>C<\/code>\u8bed\u8a00\u4e2d\u7684<code>do while<\/code>\u8bed\u53e5<\/li>\n\n\n\n<li>\u4e00\u6761<code>repeat-until<\/code>\u8bed\u53e5\u91cd\u590d\u6267\u884c\u5176\u5faa\u73af\u4f53\u76f4\u5230\u6761\u4ef6\u4e3a\u771f\u65f6\u7ed3\u675f\u3002\u68c0\u6d4b\u6761\u4ef6\u662f\u5728\u5faa\u73af\u4f53\u4e4b\u540e\u505a\u7684\uff0c\u5faa\u73af\u4f53\u4f1a\u81f3\u5c11\u6267\u884c\u4e00\u6b21<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  --\u6253\u5370\u8f93\u5165\u7684\u7b2c\u4e00\u884c\u4e0d\u4e3a\u7a7a\u7684\u5185\u5bb9--\n  repeat\n      line = io.read()\n  until line ~= \"\"\n  print(line)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 for\u5faa\u73af<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>for<\/code>\u8bed\u53e5\u6709\u4e24\u79cd\u5f62\u5f0f\uff1a\u6570\u5b57\u578b<code>for<\/code>\u548c\u6cdb\u578b<code>for<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.4.1 \u6570\u5b57\u578bfor\u5faa\u73af<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>var<\/code>\u4ece<code>exp1<\/code>\u53d8\u5316\u5230<code>exp2<\/code>\uff0c\u6bcf\u6b21\u53d8\u5316\u90fd\u4ee5<code>exp3<\/code>\u4f5c\u4e3a\u6b65\u957f\u9012\u589e<code>var<\/code>\uff0c\u5e76\u6267\u884c\u4e00\u6b21\u5faa\u73af\u4f53\u3002\u7b2c\u4e09\u4e2a\u8868\u8fbe\u5f0f<code>exp3<\/code>\u662f\u53ef\u9009\u53c2\u6570\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u7684\u8bdd\uff0c<code>Lua<\/code>\u4f1a\u5c06\u6b65\u957f\u9ed8\u8ba4\u4e3a<code>1<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  for var = exp1, exp2, exp3 do\n      &lt;\u6267\u884c\u4f53&gt;\n  end\n\n  --\u4f8b\u5982,\u4f9d\u6b21\u6253\u5370\u4ece100\u52301--\n  for i = 100, 1, -1 do\n      print(i)\n  end<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4e0d\u60f3\u7ed9\u5faa\u73af\u8bbe\u7f6e\u4e0a\u9650\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e38\u91cf<code>math.huge<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  --\u65e0\u9650\u5faa\u73af\u6253\u5370--\n  for i = 1, math.huge do\n      print(i)\n  end<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728<code>for<\/code>\u5faa\u73af\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u9000\u51fa\u5faa\u73af<\/li>\n\n\n\n<li><code>for<\/code>\u5faa\u73af\u7684\u4e09\u4e2a\u8868\u8fbe\u5f0f\u662f\u5728\u5faa\u73af\u5f00\u59cb\u524d\u4e00\u6b21\u6027\u6c42\u503c\u7684<\/li>\n\n\n\n<li>\u63a7\u5236\u53d8\u91cf\u4f1a\u88ab\u81ea\u52a8\u7684\u58f0\u660e\u4e3a<code>for<\/code>\u5faa\u73af\u8bed\u53e5\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u4e14\u4ec5\u5728\u5faa\u73af\u4f53\u5185\u53ef\u89c1\uff0c\u56e0\u6b64\u63a7\u5236\u53d8\u91cf\u5728\u5faa\u73af\u7ed3\u675f\u540e\u5c31\u4e0d\u5b58\u5728\u4e86<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.4.2 \u6cdb\u578bfor\u5faa\u73af<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6cdb\u578b<code>for<\/code>\u5faa\u73af\u901a\u8fc7\u4e00\u4e2a\u8fed\u4ee3\u5668\u51fd\u6570\u6765\u904d\u5386\u6240\u6709\u503c<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  for key, value in pairs(table_1) do      \n      print(tostring(key)..\":\"..tostring(value))\n  end<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.4.3 \u8fed\u4ee3\u5668<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u53ef\u4ee5\u904d\u5386\u96c6\u5408\u4e2d\u6240\u6709\u5143\u7d20\u7684\u673a\u5236\uff0c<code>lua<\/code>\u4e2d\u8fed\u4ee3\u5668\u4f7f\u7528\u51fd\u6570\u8868\u793a\uff0c\u6bcf\u8c03\u7528\u51fd\u6570\u4e00\u6b21\uff0c\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20<\/li>\n\n\n\n<li>\u8fed\u4ee3\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\u662f\u5b9a\u4e49\u4e00\u4e2a\u5de5\u5382\uff0c\u7136\u540e\u5229\u7528\u8fd9\u4e2a\u5de5\u5382\u751f\u4ea7\u95ed\u5305\uff0c\u7136\u540e\u8c03\u7528\u8fd9\u4e2a\u95ed\u5305\u6765\u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u7f3a\u70b9\u5c31\u662f\u6bcf\u6b21\u90fd\u5f97\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u95ed\u5305\uff0c\u6709\u4e00\u5b9a\u7684\u5f00\u9500<\/li>\n\n\n\n<li>\u4f7f\u7528\u8fed\u4ee3\u5668\u7684\u65b9\u5f0f\u4e5f\u5f88\u7b80\u5355\uff0c\u76f4\u63a5\u8c03\u7528\u8fed\u4ee3\u5668\u5c31\u76f8\u5f53\u4e8e\u8c03\u7528\u95ed\u5305\u51fd\u6570\uff0c\u8fed\u4ee3\u5668\u4f1a\u8fd4\u56de\u5143\u7d20\u96c6\u5408\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u5f53\u8fd4\u56de<code>nil<\/code>\u65f6\u5c31\u4ee3\u8868\u5143\u7d20\u904d\u5386\u5b8c\u4e86<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function value(t)\n    local i = 0\n    return function()\n        i = i + 1\n        return t&#91;i]\n    end\nend\n\nt = {1, 2, 3, 4, 5}\n-- \u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\niter = value(t)\n-- \u904d\u5386\u8fed\u4ee3\u5668\nwhile true do\n    local ele = iter()\n    if ele == nil then\n        break\n    end\n    print(ele)\nend<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65e0\u72b6\u6001\u8fed\u4ee3\u5668<\/li>\n\n\n\n<li>\u65e0\u72b6\u6001\u8fed\u4ee3\u5668\u6307\u7684\u662f\u5728\u5faa\u73af\u8fc7\u7a0b\u4e2d\uff0c\u4e0d\u4fdd\u7559\u4efb\u4f55\u72b6\u6001\u7684\u8fed\u4ee3\u5668\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u907f\u514d\u521b\u5efa\u95ed\u5305\u6240\u82b1\u8d39\u7684\u4ee3\u4ef7\u3002\u5178\u578b\u7684\u65e0\u72b6\u6001\u8fed\u4ee3\u5668\u5c31\u662f\uff1a<code>ipairs<\/code><\/li>\n\n\n\n<li>\u6cdb\u578b <code>for<\/code> \u5faa\u73af\u7684\u8bbe\u8ba1\u672c\u8eab\u5c31\u5305\u542b\u8fed\u4ee3\u5668\uff0c\u800c\u4e14\u662f\u4e00\u79cd\u65e0\u72b6\u6001\u7684\u8fed\u4ee3\u5668\uff0c\u5373\u4e0d\u9700\u8981\u521b\u5efa\u95ed\u5305<\/li>\n\n\n\n<li>\u6cdb\u578b <code>for<\/code> \u5faa\u73af\u5185\u90e8\u4fdd\u5b58\u4e86\u8fed\u4ee3\u5668\u72b6\u6001\uff0c\u5305\u62ec\u8fed\u4ee3\u5668\u51fd\u6570\u3001\u63a7\u5236\u53d8\u91cf\u548c\u6052\u5b9a\u72b6\u6001<ul><li>\u8fed\u4ee3\u5668\u51fd\u6570\uff1a\u8fed\u4ee3\u5668\u5de5\u5382\u4ea7\u751f\u7684\u533f\u540d\u51fd\u6570\uff0c\u4e0d\u662f\u95ed\u5305\u63a7\u5236\u53d8\u91cf\uff1a<code>for<\/code> \u5faa\u73af\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\u63a7\u5236\u5f53\u524d\u904d\u5386\u7684\u4f4d\u7f6e\u53ca\u4f55\u65f6\u7ed3\u675f\u904d\u5386\u6052\u5b9a\u72b6\u6001\uff1a\u8fed\u4ee3\u5668\u904d\u5386\u7684\u76ee\u6807\uff0c\u4e00\u822c\u662f\u6307<\/li><\/ul><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>iter = function(t, i)\n    i = i + 1\n    local v = t&#91;i]\n    if v then\n        return i, v\n    end\nend\n\nipairs = function(t)\n    return iter, t, 0\nend\n\nfor i, v in ipairs(t) do\n    print(i, v)\nend<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u72b6\u6001\u8fed\u4ee3\u5668<\/li>\n\n\n\n<li>\u6cdb\u578b <code>for<\/code> \u5faa\u73af\u53ef\u4ee5\u5b9e\u73b0\u53ea\u6709\u4e00\u4e2a\u6052\u5b9a\u72b6\u6001\u7684\u8fed\u4ee3\u5668\uff0c\u5982\u679c\u6709\u591a\u4e2a\u72b6\u6001\u9700\u8981\u4fdd\u5b58\u5c31\u4e0d\u884c\u4e86\u3002\u5982\u679c\u8981\u4fdd\u5b58\u591a\u4e2a\u72b6\u6001\uff0c\u53ef\u4ee5\u6709\u4e0b\u9762\u4e24\u79cd\u65b9\u5f0f<ul><li>\u4f7f\u7528\u95ed\u5305\u628a\u591a\u4e2a\u72b6\u6001\u5c01\u88c5\u5728\u4e00\u4e2a <code>table<\/code> \u91cc\u9762\uff0c\u7136\u540e\u4f7f\u7528\u6cdb\u578b <code>for<\/code> \u5faa\u73af<\/li><\/ul><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>local array = {\"Google\", \"Runoob\"}\n\nfunction elementIterator (collection)\n   local index = 0\n   local count = #collection\n   -- \u95ed\u5305\u51fd\u6570\n   return function ()\n      index = index + 1\n      if index &lt;= count\n      then\n         --  \u8fd4\u56de\u8fed\u4ee3\u5668\u7684\u5f53\u524d\u5143\u7d20\n         return collection&#91;index]\n      end\n   end\nend\n\nfor element in elementIterator(array)\ndo\n   print(element)\nend<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u51fd\u6570<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\u8c03\u7528\uff0c\u9700\u8981\u5c06\u6240\u6709\u53c2\u6570\u653e\u5230\u4e00\u5bf9\u5706\u62ec\u53f7\u4e2d\uff0c\u5373\u4f7f\u8c03\u7528\u51fd\u6570\u65f6\u6ca1\u6709\u53c2\u6570\u4e5f\u5fc5\u987b\u5199\u51fa\u4e00\u5bf9\u5706\u62ec\u53f7<\/li>\n\n\n\n<li>\u82e5\u4e00\u4e2a\u51fd\u6570\u82e5\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u4e14\u6b64\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u9762\u5b57\u7b26\u4e32\u6216<code>table<\/code>\u6784\u9020\u5f0f\uff0c\u90a3\u4e48\u5706\u62ec\u53f7\u4fbf\u662f\u53ef\u6709\u53ef\u65e0\u7684 <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>print \"Hello Morld\"         --->Hello Morld\nprint(\"Hello World\")        --->Hello Morld\n\nprint(type{})               --->table\nprint(type({}))             --->table<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u4e3a\u9762\u5411\u5bf9\u8c61\u5f0f\u7684\u8c03\u7528\u4e5f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7279\u6b8a\u7684\u8bed\u6cd5\u2014\u2014\u5192\u53f7\u64cd\u4f5c\u7b26\u3002\u8868\u8fbe\u5f0f<code>o.foo(o, x)<\/code>\u7684\u53e6\u4e00\u79cd\u5199\u6cd5\u662f<code>o:foo(x)<\/code>,\u5192\u53f7\u64cd\u4f5c\u7b26\u4f7f\u8c03\u7528<code>o.foo<\/code>\u65f6\u5c06<code>o<\/code>\u9690\u542b\u5730\u4f5c\u4e3a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/li>\n\n\n\n<li>\u5173\u4e8e\u5b9e\u53c2\u548c\u5f62\u53c2\uff0c<code>Lua<\/code>\u4f1a\u81ea\u52a8\u8c03\u6574\u5b9e\u53c2\u7684\u6570\u91cf\uff0c\u4ee5\u5339\u914d\u53c2\u6570\u8868\u7684\u8981\u6c42\u3002\u8fd9\u9879\u8c03\u6574\u4e0e\u591a\u91cd\u8d4b\u503c\u5f88\u76f8\u4f3c\uff0c\u5373\u201c\u82e5\u5b9e\u53c2\u591a\u4f59\u5f62\u53c2\uff0c\u5219\u820d\u5f03\u591a\u4f59\u7684\u5b9e\u53c2\uff1b\u82e5\u5b9e\u53c2\u4e0d\u8db3\uff0c\u5219\u591a\u4f59\u7684\u5f62\u53c2\u521d\u59cb\u5316\u4e3a<code>nil<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u591a\u91cd\u8fd4\u56de\u503c<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u5177\u6709\u4e00\u9879\u975e\u5e38\u4e0e\u4f17\u4e0d\u540c\u7684\u7279\u5f81\uff0c\u5141\u8bb8\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u7ed3\u679c\uff0c\u53ea\u9700\u5728<code>return<\/code>\u5173\u952e\u5b57\u540e\u5217\u51fa\u6240\u6709\u7684\u8fd4\u56de\u503c\u5373\u53ef<\/li>\n\n\n\n<li>\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\u6216\u8005\u6ca1\u6709\u8fd4\u56de\u8db3\u591f\u591a\u7684\u8fd4\u56de\u503c\uff0c\u90a3\u4e48<code>Lua<\/code>\u4f1a\u7528<code>nil<\/code>\u6765\u8865\u5145\u7f3a\u5931\u7684\u503c<\/li>\n\n\n\n<li><code>table<\/code>\u6784\u9020\u5f0f\u53ef\u4ee5\u5b8c\u6574\u5730\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u6240\u6709\u7ed3\u679c\uff0c\u5373\u4e0d\u4f1a\u6709\u4efb\u4f55\u6570\u91cf\u65b9\u9762\u7684\u8c03\u6574\uff1a<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 \u53d8\u957f\u53c2\u6570<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u53c2\u6570\u4f20\u9012\u65f6\u53ef\u4ee5\u5c06\u6240\u6709\u53c2\u6570\u5168\u90e8\u653e\u5728\u4e00\u4e2a<code>table<\/code>\u4e2d\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a<code>table<\/code>\u4f5c\u4e3a\u552f\u4e00\u53c2\u6570\u4f20\u9012\uff1b\u5728\u51fd\u6570\u5185\u90e8\uff0c\u5bf9\u5fc5\u8981\u53c2\u6570\u8fdb\u884c\u68c0\u67e5\uff0c\u8fd9\u6837\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u53d8\u957f\u53c2\u6570\u7684\u6548\u679c<\/li>\n\n\n\n<li><code>Lua<\/code>\u4e2d\u7684\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e0d\u540c\u6570\u91cf\u7684\u5b9e\u53c2<\/li>\n\n\n\n<li>\u53c2\u6570\u8868\u4e2d\u7684<code>3<\/code>\u4e2a\u70b9<code>...<\/code>\u8868\u793a\u8be5\u51fd\u6570\u53ef\u63a5\u53d7\u4e0d\u540c\u6570\u91cf\u7684\u5b9e\u53c2\uff0c\u4e00\u4e2a\u51fd\u6570\u8981\u8bbf\u95ee\u5b83\u7684\u53d8\u957f\u53c2\u6570\u65f6\uff0c\u4ecd\u9700\u7528\u5230<code>3<\/code>\u4e2a\u70b9<code>...<\/code>\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u6b64\u65f6\u8fd9<code>3<\/code>\u4e2a\u70b9\u662f\u4f5c\u4e3a\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6765\u4f7f\u7528\u7684<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function foo(...)\n    local a,b,c = ...\nend\n\nfunction foo(...)\n    return ...\nend<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5177\u6709\u53d8\u957f\u53c2\u6570\u7684\u51fd\u6570\u540c\u6837\u4e5f\u53ef\u4ee5\u62e5\u6709\u4efb\u610f\u6570\u91cf\u7684\u56fa\u5b9a\u53c2\u6570\uff0c\u4f46\u56fa\u5b9a\u53c2\u6570\u5fc5\u987b\u653e\u5728\u53d8\u957f\u53c2\u6570\u4e4b\u524d\u3002<code>Lua<\/code>\u4f1a\u5c06\u524d\u9762\u7684\u5b9e\u53c2\u8d4b\u4e88\u56fa\u5b9a\u53c2\u6570\uff0c\u800c\u5c06\u4f59\u4e0b\u7684\u5b9e\u53c2(\u5982\u679c\u6709\u7684\u8bdd)\u89c6\u4e3a\u53d8\u957f\u53c2\u6570<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  function foo(fmt, ...)\n      return string.format(fmt, ...)\n  end\n\n  local a, b, c = foo(\"a\")                 ---&gt;a,nil,nil\n  local a, b, c = foo(\"%d, %d\", 4, 5)      ---&gt;4,5,nil,nil<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u5e38\u4e00\u4e2a\u51fd\u6570\u5728\u904d\u5386\u5176\u53d8\u957f\u53c2\u6570\u65f6\u53ea\u9700\u4f7f\u7528\u8868\u8fbe\u5f0f<code>...<\/code>,\u8fd9\u5c31\u50cf\u8bbf\u95ee\u4e00\u4e2a<code>table<\/code>\u4e00\u6837\uff0c\u8bbf\u95ee\u6240\u6709\u7684\u53d8\u957f\u53c2\u6570<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  --select\u51fd\u6570\u7528\u4e8e\u8bbf\u95ee\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u5217\u8868\u4e2d\u7684\u53c2\u6570\n  for i = 1, select('#', ...) do      --\u904d\u5386\u6240\u6709\u53d8\u957f\u53c2\u6570\n      local arg = select(i, ...)      --\u5f97\u5230\u7b2ci\u4e2a\u53c2\u6570\n      print(arg)\n  end<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u6df1\u5165\u51fd\u6570<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728<code>Lua<\/code>\u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u79cd\u201c\u7b2c\u4e00\u7c7b\u503c\u201d,\u5b83\u4eec\u5177\u6709\u7279\u5b9a\u7684\u8bcd\u6cd5\u57df\uff0c<strong>\u7b2c\u4e00\u7c7b\u503c<\/strong>\u3002\u8fd9\u8868\u793a\u5728<code>Lua<\/code>\u4e2d\u51fd\u6570\u4e0e\u5176\u4ed6\u4f20\u7edf\u7c7b\u578b\u7684\u503c(\u4f8b\u5982\u6570\u5b57\u548c\u5b57\u7b26\u4e32)\u5177\u6709\u76f8\u540c\u7684\u6743\u5229\u3002<strong>\u51fd\u6570\u53ef\u4ee5\u5b58\u50a8\u5230\u53d8\u91cf\u4e2d<\/strong>(\u65e0\u8bba\u5168\u5c40\u53d8\u91cf\u8fd8\u662f\u5c40\u90e8\u53d8\u91cf)\u6216<code>table<\/code>\u4e2d\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  local network ={\n      {name = \"a\", IP = \"210.26.30.34\"},\n      {name = \"c\", IP = \"210.26.30.23\"},\n      {name = \"d\", IP = \"210.26.23.12\"},\n      {name = \"b\", IP = \"210.26.23.20\"},\n  }\n\n  --\u5982\u679c\u60f3\u4ee5name\u5b57\u6bb5\u3001\u6309\u53cd\u5411\u7684\u5b57\u7b26\u987a\u5e8f\u6765\u5bf9\u8fd9\u4e2atable\u6392\u5e8f\u7684\u8bdd\uff0c\u53ea\u9700\u8fd9\u4e48\u5199\uff1a\n  table.sort(network, function (a, b) return(a.name &gt; b.name) end)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Lua<\/code>\u4e2d\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u53d8\u91cf\u88ab\u5176\u4ed6\u53d8\u91cf\u6301\u6709<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 \u95ed\u5408\u51fd\u6570\uff08\u95ed\u5305\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u82e5\u5c06\u4e00\u4e2a\u51fd\u6570\u5199\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5185\uff0c\u90a3\u4e48\u8fd9\u4e2a\u4f4d\u4e8e\u5185\u90e8\u7684\u51fd\u6570\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u9879\u7279\u5f81\u79f0\u4e4b\u4e3a<strong>\u8bcd\u6cd5\u57df<\/strong><\/li>\n\n\n\n<li><code>Lua<\/code>\u4e2d\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u5d4c\u5957\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c<strong>\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u4e2d\u7684\u53d8\u91cf<\/strong><\/li>\n\n\n\n<li>\u51fd\u6570\u4f5c\u4e3a\u8868\u8fbe\u5f0f\u65f6\u4e0d\u80fd\u547d\u540d<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function foo_1(x)\n    x = x + 1\n    return function ()\n        print(x)\n        return x\n    end\nend\n\nlocal foo_3 = foo_1(10)\nfoo_3()          --->11<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5916\u90e8\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u751f\u547d\u5468\u671f\u4e0e\u666e\u901a\u51fd\u6570\u76f8\u540c\uff0c\u5728\u5916\u90e8\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u540e\u53d8\u91cf\u91ca\u653e<\/li>\n\n\n\n<li>\u5185\u90e8\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u751f\u547d\u5468\u671f\u4e0e\u5916\u90e8\u63a5\u6536\u95ed\u5305\u7684\u53d8\u91cf\u76f8\u540c<\/li>\n\n\n\n<li>\u6bcf\u6b21\u5f62\u6210\u95ed\u5305\u540e\u5185\u90e8\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u4f1a\u5355\u72ec\u7533\u8bf7\u4e00\u5757\u5185\u5b58<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 \u5c3e\u8c03\u7528\u6d88\u9664<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u662f\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u52a8\u4f5c\u65f6\uff0c\u8be5\u8c03\u7528\u7b97\u662f\u4e00\u6761\u5c3e\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  function f (x) return g(x) end<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u5916\u90e8\u51fd\u6570\u8c03\u7528\u5185\u90e8\u51fd\u6570\u540e\u5c31\u6ca1\u6709\u5176\u4ed6\u4e8b\u60c5\u53ef\u505a\uff0c\u56e0\u6b64\u7a0b\u5e8f\u4e5f\u4e0d\u9700\u8981\u4fdd\u5b58\u4efb\u4f55\u5173\u4e8e\u8be5\u51fd\u6570\u7684\u6808\u4fe1\u606f\uff0c\u53ca\u5c3e\u8c03\u7528\u4e0d\u4f1a\u6d88\u8017\u6808\u7a7a\u95f4<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u7c7b\u578b\u4e0e\u503c Lua\u662f\u4e00\u79cd\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u5728\u8bed\u8a00\u4e2d\u6ca1\u6709\u7c7b\u578b\u5b9a\u4e49\u7684\u8bed\u6cd5\uff0c\u4efb\u4f55\u53d8\u91cf\u90fd\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u7c7b\u578b\u7684\u503c\uff1bLua\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-learning-programming-languages"],"_links":{"self":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=138"}],"version-history":[{"count":7,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":159,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions\/159"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}