• Awk基本入门[5] Awk Associative Arrays


    1、Assigning Array Elements


     

    In Awk, arrays are associative, i.e. an array contains multiple index/value pairs. The index doesn't need to be a continuous set of numbers; in fact it can be a string or a number, and you don't need to specify the size of the array.

    Syntax:

    arrayname[string]=value
    • arrayname is the name of the array.
    • string is the index of an array.
    • value is any value assigning to the element of the array.

    The following is a simple array assignment example:

    $ cat array-assign.awk
    BEGIN {
      item[101]="HD Camcorder";
      item[102]="Refrigerator";
      item[103]="MP3 Player";
      item[104]="Tennis Racket";
      item[105]="Laser Printer";
      item[1001]="Tennis Ball";
      item[55]="Laptop";
      item["na"]="Not Available";
      print item["101"];
      print item[102];
      print item["103"];
      print item[104];
      print item["105"];
      print item[1001];
      print item[55];
      print item["na"];
    }
    $
    awk -f array-assign.awk HD Camcorder Refrigerator MP3 Player Tennis Racket Laser Printer Tennis Ball Laptop Not Available

    Please note the following in the above example:

    • Array indexes are not in sequence. It didn't even have to start    from 0 or 1. It really started from 101 .. 105, then jumped to   1001, then came down to 55, then it had a string index "na".
    • Array indexes can be string. The last item in this array has an    index string. i.e. "na" is the index.
    • You don't need to initialize or even define the array in awk;   you don't need to specify the total array size before you have  to use it.
    • The naming convention of an awk array is same as the naming   convention of an awk variable.


    From awk's point of view, the index of the array is always a string.Even when you pass a number for the index, awk will treat it as string index. Both of the following are the same.

    item[101]="HD Camcorder"
    item["101"]="HD Camcorder"
  • 相关阅读:
    ATL正则表达式库使用
    用InternetOpen()的下载者
    获取IWebBrowser2指针的方法
    IE自动登陆-Navigate篇
    用WinInet开发Internet客户端应用指南
    VC中的GetKeyState和GetAsyncKeyState的区别
    通过IWebBrowser2的Navigate2来打开网页,怎样判断网页是否全部加载完毕
    利用IWebBrowser2接口的Navigate2方法实现Http POST传输
    IE撤销机制CtrlZ功能会在由于Js动态改变页面元素失效
    Web安全渗透测试之信息搜集篇(下)
  • 原文地址:https://www.cnblogs.com/yangfengtao/p/3182867.html
Copyright © 2020-2023  润新知