{"id":217600,"date":"2021-05-17T09:25:48","date_gmt":"2021-05-17T01:25:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=217600"},"modified":"2021-04-28T10:26:38","modified_gmt":"2021-04-28T02:26:38","slug":"linux-lua-teacher","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-lua-teacher.html","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u7528 Lua \u64cd\u4f5c\u6587\u4ef6\u4e2d\u7684\u6570\u636e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e86\u89e3 Lua \u5982\u4f55\u5904\u7406\u6570\u636e\u7684\u8bfb\u5199\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6709\u4e9b\u6570\u636e\u662f\u4e34\u65f6\u7684\uff0c\u5b58\u50a8\u5728 RAM \u4e2d\uff0c\u53ea\u6709\u5728\u5e94\u7528\u8fd0\u884c\u65f6\u624d\u6709\u610f\u4e49\u3002\u4f46\u6709\u4e9b\u6570\u636e\u662f\u8981\u6301\u4e45\u7684\uff0c\u5b58\u50a8\u5728\u786c\u76d8\u4e0a\u4f9b\u4ee5\u540e\u4f7f\u7528\u3002\u5f53\u4f60\u7f16\u7a0b\u65f6\uff0c\u65e0\u8bba\u662f\u7b80\u5355\u7684\u811a\u672c\u8fd8\u662f\u590d\u6742\u7684\u5de5\u5177\u5957\u4ef6\uff0c\u901a\u5e38\u90fd\u9700\u8981\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002\u6709\u65f6\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u914d\u7f6e\u9009\u9879\uff0c\u800c\u53e6\u4e00\u4e9b\u65f6\u5019\u8fd9\u4e2a\u6587\u4ef6\u662f\u4f60\u7684\u7528\u6237\u7528\u4f60\u7684\u5e94\u7528\u521b\u5efa\u7684\u6570\u636e\u3002\u6bcf\u79cd\u8bed\u8a00\u90fd\u4f1a\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5904\u7406\u8fd9\u9879\u4efb\u52a1\uff0c\u672c\u6587\u5c06\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Lua \u5904\u7406\u6587\u4ef6\u6570\u636e\u3002<\/p>\n

\u5b89\u88c5 Lua<\/strong><\/div>\n

\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f Linux\uff0c\u4f60\u53ef\u4ee5\u4ece\u4f60\u7684\u53d1\u884c\u7248\u8f6f\u4ef6\u5e93\u4e2d\u5b89\u88c5 Lua\u3002\u5728 macOS \u4e0a\uff0c\u4f60\u53ef\u4ee5\u4ece MacPorts \u6216 Homebrew \u5b89\u88c5 Lua\u3002\u5728 Windows \u4e0a\uff0c\u4f60\u53ef\u4ee5\u4ece Chocolatey \u5b89\u88c5 Lua\u3002<\/p>\n

\u5b89\u88c5 Lua \u540e\uff0c\u6253\u5f00\u4f60\u6700\u559c\u6b22\u7684\u6587\u672c\u7f16\u8f91\u5668\u5e76\u51c6\u5907\u5f00\u59cb\u3002<\/p>\n

\u7528 Lua \u8bfb\u53d6\u6587\u4ef6<\/strong><\/div>\n

Lua \u4f7f\u7528 io \u5e93\u8fdb\u884c\u6570\u636e\u8f93\u5165\u548c\u8f93\u51fa\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a ingest \u7684\u51fd\u6570\u6765\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u7136\u540e\u7528 :read \u51fd\u6570\u8fdb\u884c\u89e3\u6790\u3002\u5728 Lua \u4e2d\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u65f6\uff0c\u6709\u51e0\u79cd\u6a21\u5f0f\u53ef\u4ee5\u542f\u7528\u3002\u56e0\u4e3a\u6211\u53ea\u9700\u8981\u4ece\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u6240\u4ee5\u6211\u4f7f\u7528 r\uff08\u4ee3\u8868\u201c\u8bfb\u201d\uff09\u6a21\u5f0f\uff1a<\/p>\n

\r\nfunction ingest(file)\r\n   local f = io.open(file, \"r\")\r\n   local lines = f:read(\"*all\")\r\n   f:close()\r\n   return(lines)\r\nend\r\nmyfile=ingest(\"example.txt\")\r\nprint(myfile)<\/pre>\n

\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6ce8\u610f\u5230\u53d8\u91cf myfile \u662f\u4e3a\u4e86\u89e6\u53d1 ingest \u51fd\u6570\u800c\u521b\u5efa\u7684\uff0c\u56e0\u6b64\uff0c\u5b83\u63a5\u6536\u8be5\u51fd\u6570\u8fd4\u56de\u7684\u4efb\u4f55\u5185\u5bb9\u3002ingest \u51fd\u6570\u8fd4\u56de\u6587\u4ef6\u7684\u884c\u6570\uff08\u4ece\u4e00\u4e2a\u79f0\u4e3a lines \u7684\u53d8\u91cf\u4e2d0\u3002\u5f53\u6700\u540e\u4e00\u6b65\u6253\u5370 myfile \u53d8\u91cf\u7684\u5185\u5bb9\u65f6\uff0c\u6587\u4ef6\u7684\u884c\u6570\u5c31\u4f1a\u51fa\u73b0\u5728\u7ec8\u7aef\u4e2d\u3002<\/p>\n

\u5982\u679c\u6587\u4ef6 example.txt \u4e2d\u5305\u542b\u4e86\u914d\u7f6e\u9009\u9879\uff0c\u90a3\u4e48\u6211\u4f1a\u5199\u4e00\u4e9b\u989d\u5916\u7684\u4ee3\u7801\u6765\u89e3\u6790\u8fd9\u4e9b\u6570\u636e\uff0c\u53ef\u80fd\u4f1a\u4f7f\u7528\u53e6\u4e00\u4e2a Lua \u5e93\uff0c\u8fd9\u53d6\u51b3\u4e8e\u914d\u7f6e\u662f\u4ee5 INI \u6587\u4ef6\u8fd8\u662f YAML \u6587\u4ef6\u6216\u5176\u4ed6\u683c\u5f0f\u5b58\u50a8\u3002\u5982\u679c\u6570\u636e\u662f SVG \u56fe\u5f62\uff0c\u6211\u4f1a\u5199\u989d\u5916\u7684\u4ee3\u7801\u6765\u89e3\u6790 XML\uff0c\u53ef\u80fd\u4f1a\u4f7f\u7528 Lua \u7684 SVG \u5e93\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u7684\u4ee3\u7801\u8bfb\u53d6\u7684\u6570\u636e\u4e00\u65e6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u5c31\u53ef\u4ee5\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u9700\u8981\u52a0\u8f7d io \u5e93\u3002<\/p>\n

\u7528 Lua \u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6<\/strong><\/div>\n

\u65e0\u8bba\u4f60\u662f\u8981\u5b58\u50a8\u7528\u6237\u7528\u4f60\u7684\u5e94\u7528\u521b\u5efa\u7684\u6570\u636e\uff0c\u8fd8\u662f\u4ec5\u4ec5\u662f\u5173\u4e8e\u7528\u6237\u5728\u5e94\u7528\u4e2d\u505a\u4e86\u4ec0\u4e48\u7684\u5143\u6570\u636e\uff08\u4f8b\u5982\uff0c\u6e38\u620f\u4fdd\u5b58\u6216\u6700\u8fd1\u64ad\u653e\u7684\u6b4c\u66f2\uff09\uff0c\u90fd\u6709\u5f88\u591a\u5f88\u597d\u7684\u7406\u7531\u6765\u5b58\u50a8\u6570\u636e\u4f9b\u4ee5\u540e\u4f7f\u7528\u3002\u5728 Lua \u4e2d\uff0c\u8fd9\u662f\u901a\u8fc7 io \u5e93\u5b9e\u73b0\u7684\uff0c\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5c06\u6570\u636e\u5199\u5165\u5176\u4e2d\uff0c\u7136\u540e\u5173\u95ed\u6587\u4ef6\uff1a<\/p>\n

function exgest(file)\r\n   local f = io.open(file, \"a\")\r\n   io.output(f)\r\n   io.write(\"hello world\\n\")\r\n   io.close(f)\r\nend\r\nexgest(\"example.txt\")<\/pre>\n

\u4e3a\u4e86\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u6211\u4ee5 r \u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u4f46\u8fd9\u6b21\u6211\u4f7f\u7528 a \uff08\u7528\u4e8e\u201d\u8ffd\u52a0\u201c\uff09\u5c06\u6570\u636e\u5199\u5230\u6587\u4ef6\u7684\u672b\u5c3e\u3002\u56e0\u4e3a\u6211\u662f\u5c06\u7eaf\u6587\u672c\u5199\u5165\u6587\u4ef6\uff0c\u6240\u4ee5\u6211\u6dfb\u52a0\u4e86\u81ea\u5df1\u7684\u6362\u884c\u7b26\uff08\/n\uff09\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f60\u5e76\u4e0d\u662f\u5c06\u539f\u59cb\u6587\u672c\u5199\u5165\u6587\u4ef6\uff0c\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u5e93\u6765\u4ee3\u66ff\u5199\u5165\u4e00\u4e2a\u7279\u5b9a\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528 INI \u6216 YAML \u5e93\u6765\u5e2e\u52a9\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\uff0c\u4f7f\u7528 XML \u5e93\u6765\u7f16\u5199 XML\uff0c\u7b49\u7b49\u3002<\/p>\n

\u6587\u4ef6\u6a21\u5f0f<\/strong><\/div>\n

\u5728 Lua \u4e2d\u6253\u5f00\u6587\u4ef6\u65f6\uff0c\u6709\u4e00\u4e9b\u4fdd\u62a4\u63aa\u65bd\u548c\u53c2\u6570\u6765\u5b9a\u4e49\u5982\u4f55\u5904\u7406\u6587\u4ef6\u3002\u9ed8\u8ba4\u503c\u662f r\uff0c\u5141\u8bb8\u4f60\u53ea\u8bfb\u6570\u636e\uff1a<\/p>\n

r \u53ea\u8bfb<\/p>\n

w \u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u8986\u76d6\u6216\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n

r+ \u8bfb\u53d6\u548c\u8986\u76d6\u3002<\/p>\n

a \u8ffd\u52a0\u6570\u636e\u5230\u6587\u4ef6\u4e2d\uff0c\u6216\u5728\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n

a+ \u8bfb\u53d6\u6570\u636e\uff0c\u5c06\u6570\u636e\u8ffd\u52a0\u5230\u6587\u4ef6\u4e2d\uff0c\u6216\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u8bdd\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n

\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684(\u4f8b\u5982\uff0cb \u4ee3\u8868\u4e8c\u8fdb\u5236\u683c\u5f0f)\uff0c\u4f46\u8fd9\u4e9b\u662f\u6700\u5e38\u89c1\u7684\u3002\u5173\u4e8e\u5b8c\u6574\u7684\u6587\u6863\uff0c\u8bf7\u53c2\u8003 Lua.org\/manual \u4e0a\u7684\u4f18\u79c0 Lua \u6587\u6863\u3002<\/p>\n

Lua \u548c\u6587\u4ef6<\/strong><\/div>\n

\u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e00\u6837\uff0cLua \u6709\u5927\u91cf\u7684\u5e93\u652f\u6301\u6765\u8bbf\u95ee\u6587\u4ef6\u7cfb\u7edf\u6765\u8bfb\u5199\u6570\u636e\u3002\u56e0\u4e3a Lua \u6709\u4e00\u4e2a\u4e00\u81f4\u4e14\u7b80\u5355\u8bed\u6cd5\uff0c\u6240\u4ee5\u5f88\u5bb9\u6613\u5bf9\u4efb\u4f55\u683c\u5f0f\u7684\u6587\u4ef6\u6570\u636e\u8fdb\u884c\u590d\u6742\u7684\u5904\u7406\u3002\u8bd5\u7740\u5728\u4f60\u7684\u4e0b\u4e00\u4e2a\u8f6f\u4ef6\u9879\u76ee\u4e2d\u4f7f\u7528 Lua\uff0c\u6216\u8005\u4f5c\u4e3a C \u6216 C++ \u9879\u76ee\u7684 API\u3002<\/p>\n

via: https:\/\/opensource.com\/article\/21\/3\/lua-files<\/p>\n

\u4f5c\u8005\uff1aSeth Kenlon \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1ageekpi \u6821\u5bf9\uff1awxy<\/p>\n

\u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6709\u4e9b\u6570\u636e\u662f\u4e34\u65f6\u7684\uff0c\u5b58\u50a8\u5728 RAM \u4e2d\uff0c\u53ea\u6709\u5728\u5e94\u7528\u8fd0\u884c\u65f6\u624d\u6709\u610f\u4e49\u3002\u4f46\u6709\u4e9b\u6570\u636e\u662f\u8981\u6301\u4e45\u7684\uff0c\u5b58\u50a8\u5728\u786c\u76d8\u4e0a\u4f9b\u4ee5\u540e\u4f7f\u7528\u3002 […]<\/p>\n","protected":false},"author":1482,"featured_media":212846,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-217600","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\/217600","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\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=217600"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217600\/revisions"}],"predecessor-version":[{"id":217603,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217600\/revisions\/217603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/212846"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=217600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=217600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=217600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}