{"id":120,"date":"2022-08-18T20:08:47","date_gmt":"2022-08-18T12:08:47","guid":{"rendered":"https:\/\/courtship.top\/?p=120"},"modified":"2022-08-18T20:08:47","modified_gmt":"2022-08-18T12:08:47","slug":"lua%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2022\/08\/18\/lua%e7%ac%94%e8%ae%b0\/","title":{"rendered":"Lua\u7b14\u8bb0"},"content":{"rendered":"\n<p>\u811a\u672c\u8bed\u8a00\u771f\u968f\u610f\u554a...\u611f\u89c9\u5230\u5904\u90fd\u662f\u8bed\u6cd5\u7cd6<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5224\u65ad \u5faa\u73af<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>--&#91;&#91;\nduohangzhushi\na\na\n]]\n\n--\u8d4b\u503c\u8bed\u53e5\n--a = 123\n--b = 'abc'\n\n--\u540c\u65f6\u8d4b\u503c\na,b = 123,'abc'\n\nprint(a)\nprint(b)\n\nage = 17\nprint(type(age > 18))\n\n--false nil\u662f\u4e0d\u6210\u7acb\u7684\n--0 \u662f\u6210\u7acb\u7684\nif age &lt; 18 then\n    print(\"you can work\")\nelseif age > 75 then\n    print('go to park')\nelse\n    print('u should go to work')\nend\n\n--for\u5faa\u73af\nfor i = 1,5 do\n    print(i)\nend\n\n--while\u5faa\u73af\nwhile a > 120 do\n    print(a)\n    a = a - 1\n    if a &lt; 121 then\n        break\n    end\nend\n\n--\u4e0d\u7b49\u4e8e ~=\n\n--\u903b\u8f91 \u4e0e \u6216 \u975e and or not\n\nif not (age > 18) and age &lt; 70 then\n    print('aaaaaa')\nend\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">repeat<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ans = 15\n\nrepeat\n    print('guess the password (little than 100)')\n    a = tonumber(io.read())\n\n    if a == ans then\n        print(\"u are right!\")\n        break\n    elseif a > ans then\n        print('too big')\n    else\n        print('too small')\n    end\n\nuntil false\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">table<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>--lua\u552f\u4e00\u5185\u7f6e\u6570\u636e\u7ed3\u6784table\n\n--\u4e0b\u6807\u4ece1\u5f00\u59cb\na = {1,2,3,4,'a','b',x = 123,y = 456,pos = {c = 233}}\n\nprint(a&#91;1])\nprint(a&#91;'b'])\nprint(a&#91;'x'])\n\n--table.key \u8bed\u6cd5\u7cd6\n\na.z = 789\n--a&#91;'z'] = 789\nprint(a&#91;'z'])\nprint(a.pos.c)\n\n--\u904d\u5386 pairs ipairs\n\nfor k,v in pairs(a) do\n   print(k,' : ',v)\nend<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">function<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>--\u51fd\u6570\n--&#91;&#91;\n    add = function(a,b)\n        return a+b\n    end\n\n    print(add(1,2))\n]]\n\n\n--\u8bed\u6cd5\u7cd6\n--\u591a\u8fd4\u56de\u503c\n\nfunction fun(a,b)\n    return a + b,a * b\nend\n\n--print(add(1,2))\n\nsum,product = fun(1,2,3)\n\n--print(sum,product)\n\n--\u9ad8\u9636\u51fd\u6570\n--first class\n\nfunction process(f,a,b)\n    --print(f)\n    print(a)\n    print(b)\n    print(f(a,b))\nend\n\n--process(fun,3,5)\n--process(fun,2,4)\n\n\n--\u53ef\u53d8\u53c2\u6570\nfunction add(...)\n    local sum = 0\n    local arg = {...}\n    for k,v in ipairs(arg) do\n        sum = sum + v\n    end\n    return #arg,sum\nend\n\nprint(add(1,2,3,4,5,6))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u811a\u672c\u8bed\u8a00\u771f\u968f\u610f\u554a&#8230;\u611f\u89c9\u5230\u5904\u90fd\u662f\u8bed\u6cd5\u7cd6 \u5224\u65ad \u5faa\u73af repeat table function<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[24],"tags":[],"class_list":["post-120","post","type-post","status-publish","format-standard","hentry","category-lua"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}