{"id":201886,"date":"2020-10-06T09:09:06","date_gmt":"2020-10-06T01:09:06","guid":{"rendered":"https:\/\/gulass.cn\/?p=201886"},"modified":"2020-09-25T13:11:45","modified_gmt":"2020-09-25T05:11:45","slug":"awk-array-introduction","status":"publish","type":"post","link":"https:\/\/gulass.cn\/awk-array-introduction.html","title":{"rendered":"AWK \u6570\u7ec4\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nAWK \u53ef\u4ee5\u4f7f\u7528\u5173\u8054\u6570\u7ec4\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7d22\u5f15\u53ef\u4ee5\u662f\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u3002AWK\u5173\u8054\u6570\u7ec4\u4e5f\u4e0d\u9700\u8981\u63d0\u524d\u58f0\u660e\u5176\u5927\u5c0f\uff0c\u56e0\u4e3a\u5b83\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u81ea\u52a8\u7684\u589e\u5927\u6216\u51cf\u5c0f\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u6570\u7ec4\u4f7f\u7528\u7684\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n

array_name[index]=value<\/pre>\n
    \n
  1. array_name\uff1a\u6570\u7ec4\u7684\u540d\u79f0<\/li>\n
  2. index\uff1a\u6570\u7ec4\u7d22\u5f15<\/li>\n
  3. value\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6240\u8d4b\u4e88\u7684\u503c<\/li>\n<\/ol>\n
    \u521b\u5efa\u6570\u7ec4<\/strong><\/div>\n

    \u63a5\u4e0b\u6765\u770b\u4e00\u4e0b\u5982\u4f55\u521b\u5efa\u6570\u7ec4\u4ee5\u53ca\u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1a<\/p>\n

    $ awk 'BEGIN {\r\nsites[\"runoob\"]=\"www.runoob.com\";\r\nsites[\"google\"]=\"www.google.com\"\r\nprint sites[\"runoob\"] \"\\n\" sites[\"google\"]\r\n}'<\/pre>\n

    \u6267\u884c\u4ee5\u4e0a\u547d\u4ee4\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

    www.runoob.com\r\nwww.google.com<\/pre>\n

    \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7ad9\u70b9(sites)\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u7684\u7d22\u5f15\u4e3a\u7f51\u7ad9\u82f1\u6587\u7b80\u79f0\uff0c\u503c\u4e3a\u7f51\u7ad9\u8bbf\u95ee\u5730\u5740\u3002\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u683c\u5f0f\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1a<\/p>\n

    array_name[index]<\/pre>\n
    \u5220\u9664\u6570\u7ec4\u5143\u7d20<\/strong><\/div>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 delete \u8bed\u53e5\u6765\u5220\u9664\u6570\u7ec4\u5143\u7d20\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

    delete array_name[index<\/pre>\n

    \u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6570\u7ec4\u4e2d\u7684 google \u5143\u7d20\u88ab\u5220\u9664\uff08\u5220\u9664\u547d\u4ee4\u6ca1\u6709\u8f93\u51fa\uff09\uff1a<\/p>\n

    $ awk 'BEGIN {\r\nsites[\"runoob\"]=\"www.runoob.com\";\r\nsites[\"google\"]=\"www.google.com\"\r\ndelete sites[\"google\"];\r\nprint fruits[\"google\"]\r\n}'<\/pre>\n
    \u591a\u7ef4\u6570\u7ec4<\/strong><\/div>\n

    AWK \u672c\u8eab\u4e0d\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4f7f\u7528\u4e00\u7ef4\u6570\u7ec4\u6a21\u62df\u5b9e\u73b0\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n

    \u5982\u4e0b\u793a\u4f8b\u4e3a\u4e00\u4e2a 3x3 \u7684\u4e09\u7ef4\u6570\u7ec4\uff1a<\/p>\n

    100 200 300\r\n400 500 600\r\n700 800 900<\/pre>\n

    \u4ee5\u4e0a\u5b9e\u4f8b\u4e2d\uff0carray[0][0] \u5b58\u50a8 100\uff0carray[0][1] \u5b58\u50a8 200 \uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002\u4e3a\u4e86\u5728 array[0][0] \u5904\u5b58\u50a8 100, \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bed\u6cd5\uff1a array[\"0,0\"] = 100\u3002<\/p>\n

    \u6211\u4eec\u4f7f\u7528\u4e86 0,0 \u4f5c\u4e3a\u7d22\u5f15\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u662f\u4e24\u4e2a\u7d22\u5f15\u503c\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5b83\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u7d22\u5f15 0,0\u3002<\/p>\n

    \u4e0b\u9762\u662f\u6a21\u62df\u4e8c\u7ef4\u6570\u7ec4\u7684\u4f8b\u5b50\uff1a<\/p>\n

    $ awk 'BEGIN {\r\narray[\"0,0\"] = 100;\r\narray[\"0,1\"] = 200;\r\narray[\"0,2\"] = 300;\r\narray[\"1,0\"] = 400;\r\narray[\"1,1\"] = 500;\r\narray[\"1,2\"] = 600;\r\n# \u8f93\u51fa\u6570\u7ec4\u5143\u7d20\r\nprint \"array[0,0] = \" array[\"0,0\"];\r\nprint \"array[0,1] = \" array[\"0,1\"];\r\nprint \"array[0,2] = \" array[\"0,2\"];\r\nprint \"array[1,0] = \" array[\"1,0\"];\r\nprint \"array[1,1] = \" array[\"1,1\"];\r\nprint \"array[1,2] = \" array[\"1,2\"];\r\n}'<\/pre>\n

    \u6267\u884c\u4e0a\u9762\u7684\u547d\u4ee4\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n

    array[0,0] = 100\r\narray[0,1] = 200\r\narray[0,2] = 300\r\narray[1,0] = 400\r\narray[1,1] = 500\r\narray[1,2] = 600<\/pre>\n

    \u5728\u6570\u7ec4\u4e0a\u53ef\u4ee5\u6267\u884c\u5f88\u591a\u64cd\u4f5c\uff0c\u6bd4\u5982\uff0c\u4f7f\u7528 asort \u5b8c\u6210\u6570\u7ec4\u5143\u7d20\u7684\u6392\u5e8f\uff0c\u6216\u8005\u4f7f\u7528 asorti \u5b9e\u73b0\u6570\u7ec4\u7d22\u5f15\u7684\u6392\u5e8f\u7b49\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u6570\u7ec4\u4f7f\u7528\u7684\u8bed\u6cd5\u683c\u5f0f\uff1a array_name[index]=value array_name\uff1a\u6570\u7ec4\u7684\u540d\u79f0 ind […]<\/p>\n","protected":false},"author":321,"featured_media":201888,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-201886","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\/201886","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=201886"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/201886\/revisions"}],"predecessor-version":[{"id":201890,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/201886\/revisions\/201890"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/201888"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=201886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=201886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=201886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}