{"id":221439,"date":"2021-07-07T10:00:12","date_gmt":"2021-07-07T02:00:12","guid":{"rendered":"https:\/\/gulass.cn\/?p=221439"},"modified":"2021-06-25T09:57:25","modified_gmt":"2021-06-25T01:57:25","slug":"understand-how-to","status":"publish","type":"post","link":"https:\/\/gulass.cn\/understand-how-to.html","title":{"rendered":"\u4e86\u89e3\u4e0b\u5982\u4f55\u5728lua \u4e2d\u6c42 table \u957f\u5ea6"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5b98\u65b9\u6587\u6863\u662f\u8fd9\u4e48\u63cf\u8ff0 # \u7684\uff1a\u53d6\u957f\u5ea6\u64cd\u4f5c\u7b26\u5199\u4f5c\u4e00\u5143\u64cd\u4f5c #\u3002 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f\u5b83\u7684\u5b57\u8282\u6570\uff08\u5c31\u662f\u4ee5\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u8282\u8ba1\u7b97\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\uff09\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

table t \u7684\u957f\u5ea6\u88ab\u5b9a\u4e49\u6210\u4e00\u4e2a\u6574\u6570\u4e0b\u6807 n \u3002 \u5b83\u6ee1\u8db3 t[n] \u4e0d\u662f nil \u800c t[n+1] \u4e3a nil\uff1b \u6b64\u5916\uff0c\u5982\u679c t[1] \u4e3a nil \uff0cn \u5c31\u53ef\u80fd\u662f\u96f6\u3002 \u5bf9\u4e8e\u5e38\u89c4\u7684\u6570\u7ec4\uff0c\u91cc\u9762\u4ece 1 \u5230 n \u653e\u7740\u4e00\u4e9b\u975e\u7a7a\u7684\u503c\u7684\u65f6\u5019\uff0c \u5b83\u7684\u957f\u5ea6\u5c31\u7cbe\u786e\u7684\u4e3a n\uff0c\u5373\u6700\u540e\u4e00\u4e2a\u503c\u7684\u4e0b\u6807\u3002 \u5982\u679c\u6570\u7ec4\u6709\u4e00\u4e2a\"\u7a7a\u6d1e\" \uff08\u5c31\u662f\u8bf4\uff0cnil \u503c\u88ab\u5939\u5728\u975e\u7a7a\u503c\u4e4b\u95f4\uff09\uff0c \u90a3\u4e48 #t \u53ef\u80fd\u662f\u6307\u5411\u4efb\u4f55\u4e00\u4e2a\u662f nil \u503c\u7684\u524d\u4e00\u4e2a\u4f4d\u7f6e\u7684\u4e0b\u6807 \uff08\u5c31\u662f\u8bf4\uff0c\u4efb\u4f55\u4e00\u4e2anil \u503c\u90fd\u6709\u53ef\u80fd\u88ab\u5f53\u6210\u6570\u7ec4\u7684\u7ed3\u675f\uff09\u3002<\/p>\n

local tblTest2 =\r\n{\r\n    1,\r\n    a = 2,\r\n    3,\r\n}\r\n\r\nprint(table.getn(tblTest2))<\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u662f\u591a\u5c11\uff1f\u8fd9\u91cc\u7684\u8f93\u51fa\u7ed3\u679c\u5e94\u8be5\u662f 2\u3002\u9996\u5148\uff0c\u8981\u660e\u767d\uff0c\u8fd9\u4e2actblTest2c\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684ctable\uff0c\u5b83\u6df7\u5408\u4e86\u5217\u8868(list)\u548c\u8bb0\u5f55(record)\u4e24\u79cd\u98ce\u683c\uff0c\u8868\u4e2d\uff0ca = 2 \u662f record \u98ce\u683c\u3002\u5176\u6b21\uff0c\u8981\u660e\u767d\uff0crecord \u98ce\u683c\u7684 record \u662f\u4e0d\u4f5c\u4e3a\u5916\u8868\u7684\u957f\u5ea6\u8ba1\u7b97\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u6210\u4e00\u4e2a\u51fd\u6570\uff0c\u8ddf\u5176\u4ed6\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u4e00\u6837\uff0c\u51fd\u6570\u662f\u4e0d\u8bb0\u4e3a\u5185\u90e8\u53d8\u91cf\u7684\u3002<\/p>\n

\u65e2\u7136\u50cf\u51fd\u6570\u4e00\u6837\uff0c\u90a3\u5c31\u53ef\u4ee5\u8f93\u51faa\u7684\u503c\uff0c\u662f\u7684\u3002print(tblTest2.a) \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n

\u518d\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n

local tblTest3 =\r\n{\r\n    1,\r\n    {a = 2},\r\n    3,\r\n}\r\n\r\nprint(table.getn(tblTest3))<\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u662f\u591a\u5c11\uff1f\u8fd9\u91cc\u7684\u8f93\u51fa\u7ed3\u679c\u5e94\u8be5\u662f 3\u3002\u8981\u6ce8\u610f\uff0c\u8868\u5d4c\u5957\u8868\uff0c\u5d4c\u5957\u8868\u4e5f\u662f\u5143\u7d20\u3002\u6240\u4ee5\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u662f 3\u3002<\/p>\n

table.getn(t) \u7b49\u4ef7\u4e8e #t\u3002<\/p>\n

\u4ee5\u4e0b\u60c5\u51b5\u76f8\u5f53\u7ea0\u7ed3\uff0c\u53ef\u76f4\u63a5\u770b\u6700\u540e\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a<\/p>\n

\u73b0\u5728\u6765\u770b\u4e00\u4e2a\u6bd4\u8f83\u7ea0\u7ed3\u7684\uff1a<\/p>\n

local tblTest4 =\r\n{\r\n    1,\r\n    nil,\r\n}\r\n\r\nprint(table.getn(tblTest4))<\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u662f\u591a\u5c11\uff1f\u662f1\u3002\u6211\u4eec\u90fd\u77e5\u9053table\u83b7\u53d6\u957f\u5ea6\u7684\u65f6\u5019\uff0c\u4f1a\u904d\u5386\u4e00\u4e0b\u6574\u4e2a\u8868\uff0c\u5728\u6700\u540e\u4e00\u4e2a\u975enil\u5904\uff0c\u5c31\u4f1a\u8fd4\u56de\u3002<\/p>\n

\u4f46\u662f\uff0c\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u5462\uff1f<\/p>\n

local tblTest5 =\r\n{\r\n    1,\r\n    nil,\r\n    2,\r\n}\r\n\r\nprint(table.getn(tblTest5))<\/pre>\n

\u8f93\u51fa 3\uff0c\u597d\u73a9\u5427\uff1f\u5b83\u628a nil \u4e5f\u5f53\u6210\u5143\u7d20\u8ba1\u7b97\u957f\u5ea6\u4e86\u3002\u4f46\u53eb\u4f60\u6478\u4e0d\u7740\u5934\u8111\u7684\u662f\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n

local tblTest5 =\r\n{\r\n    1,\r\n    nil,\r\n    2,\r\n    nil\r\n}<\/pre>\n

\u8f93\u51fa1.\u518d\u53d1\u4e00\u6bb5\uff0c\u8ba9\u4f60\u5b8c\u5168\u8499\u6389\uff1a<\/p>\n

local tblTest5 =\r\n{\r\n    1,\r\n    nil,\r\n    2,\r\n    nil,\r\n    3,\r\n    nil\r\n}<\/pre>\n

\u8f93\u51fa 3\uff0c\u8499\u4e86\u6ca1\uff1f\u518d\u770b\uff0c\u8fd9\u4e00\u6bb5\u53eb\u4f60\u4ee5\u540e\u518d\u4e5f\u4e0d\u6562\u5728 table \u4e2d\u5199 nil \u503c\uff1a<\/p>\n

local tblTest5 =\r\n{\r\n    1,\r\n    nil,\r\n    2,\r\n    nil,\r\n    3,\r\n    nil,\r\n    4,\r\n    nil\r\n}<\/pre>\n

\u770b\u770b\u5427\uff0c\u8fd9\u4e00\u6bb5\u7684\u8f93\u51fa\u7ed3\u679c\u662f 1\u3002\u4eb2\uff0c\u8bf7\u95ee\uff0c\u4f60\u4ee5\u540e\u8fd8\u6562\u5728 lua \u7684 table \u4e2d\u7528 nil \u503c\u5417\uff1f\uff1f\uff1f\u5982\u679c\u4f60\u7ee7\u7eed\u5f80\u540e\u9762\u52a0 nil\uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u70b9\u4ec0\u4e48\u3002\u4f60\u53ef\u80fd\u8ba4\u4e3a\u4f60\u53d1\u73b0\u7684\u662f\u4e2a\u89c4\u5f8b\u3002\u4f46\u662f\uff0c\u4f60\u5343\u4e07\u4e0d\u8981\u8ba4\u4e3a\u8fd9\u662f\u4e2a\u89c4\u5f8b\u3002\u56e0\u4e3a\u8fd9\u662f\u9519\u8bef\u7684\u3002<\/p>\n

    \n
  1. \u5728 table \u4e2d\u4e0d\u8981\u4f7f\u7528 nil<\/li>\n
  2. \u5982\u679c\u975e\u8981\u4f7f\u7528 nil\uff0c\u5fc5\u987b\u7528 table.setn() \u51fd\u6570\u53bb\u8bbe\u7f6e\u8fd9\u4e2a table \u8868\u7684\u957f\u5ea6\u3002\u6ce8\u610f\uff1a\u65b0\u7248\u672c\u7684 lua \u5df2\u7ecf\u4e0d\u652f\u6301 setn\u4e86\u3002<\/li>\n<\/ol>\n

    \u5fc5\u987b\u7ed9\u4f60\u4e2a\u7ed3\u8bba\uff1asetn \u51fd\u6570\u5df2\u8fc7\u65f6\uff0c\u4e0d\u8981\u5728 lua \u7684 table \u4e2d\u4f7f\u7528 nil \u503c\uff0c\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u8981\u5220\u9664\uff0c\u76f4\u63a5 remove\uff0c\u4e0d\u8981\u7528 nil \u53bb\u4ee3\u66ff\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    table t \u7684\u957f\u5ea6\u88ab\u5b9a\u4e49\u6210\u4e00\u4e2a\u6574\u6570\u4e0b\u6807 n \u3002 \u5b83\u6ee1\u8db3 t[n] \u4e0d\u662f nil \u800c t[n+1] \u4e3a ni […]<\/p>\n","protected":false},"author":321,"featured_media":221445,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221439","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/221439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=221439"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/221439\/revisions"}],"predecessor-version":[{"id":221447,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/221439\/revisions\/221447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/221445"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=221439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=221439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=221439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}