Chapter 12. Awk Associative Arrays (associative array)

77. Assigning Array Elements (assignment of array elements)

grammar:

      
      
1
      
      
arrayname[ string]=value

  • Arrayname array of name
  • Directory string array subscript
  • value the value of
    the subscript of the array need not be a sequence, and the like from 0-10, can be a string. It can be equivalent to the dictionary. And pythonalmost

    78. Referring to Array Elements (reference array elements)

    When defining an array, even if there is no value, it can also be called. Because the system is assigned a null value
            
            
    1
    2
    3
    4
    5
    6
    7
            
            
    awk 'BEGIN {
    x=item[105];
    for (105 in item)
    print "yes";
    }'
    yes

It defined item[105], but there is no value, but also the call succeeds

79. Browse the Array using For Loop (cycle through the use of arrays)

grammar

      
      
1
      
      
for (var in arrayname) actions

80. Delete Array Element (delete array elements)

grammar

      
      
1
      
      
delete arrayname[index];

Cyclic array may also be used to delete all the elements of
the In GAWK , can be used delete arraynameto delete all array elements

81. Multi Dimensional Array(多维数组)

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      
      
$ cat array-multi3.awk
BEGIN {
item["1,1"]=10;
item["1,2"]=20;
item[2,1]=30;
item[2,2]=40;
for (x in item)
print "Index",x,"contains",item[x];
}
$ awk -f array-multi3.awk
Index 1,1 contains 10
Index 1,2 contains 20
Index 2#1 contains 30
Index 2#2 contains 40

下标使用""时,里面的都算一个下标
而没有分号,使用逗号,时,则为多维数组

82. SUBSEP - Subscript Separator(下标分隔符)

下标分隔符默认为#

83. Sort Array Values using asort(使用asort排序数组的值)

asort函数是将数组的值进行排序后,将下标用数字1-n表示

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
      
      
$ cat asort.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 "------Before asort------"
for (x in item)
print "Index",x,"contains",item[x];
total = asort(item);
print "------After asort------"
for (x in item)
print "Index",x,"contains",item[x];
print "Return value from asort:", total;
}
$ awk -f asort.awk
------Before asort-----
Index 55 contains Laptop
Index 101 contains HD Camcorder
Index 102 contains Refrigerator
Index 103 contains MP3 Player
Index 104 contains Tennis Racket
Index 105 contains Laser Printer
Index na contains Not Available
Index 1001 contains Tennis Ball
------After asort-----
Index 4 contains MP3 Player
Index 5 contains Not Available
Index 6 contains Refrigerator
Index 7 contains Tennis Ball
Index 8 contains Tennis Racket
Index 1 contains HD Camcorder
Index 2 contains Laptop
Index 3 contains Laser Printer
Return value from asort: 8

See the above example, the output is not sorted sequentially output, because
for (x in item)the output is unordered, can
for (i=1; i<= total; i++)be ordered output

asort does not modify the original value of the assignment

The following example will not modify the original itemarray, and creates a new array itemnewto sort

      
      
1
      
      
total = asort(item, itemnew);

84. Sort Array Indexes using (using asorti asortivalue of ordering indexs)

And asortusage, in that it is not the same sort of objects

Note that if you use asorti(array), it will replace the value of the original array into a sort of post-indexs. So be careful to use, multi-useasorti(array,arraynew)

Original: Large columns  Chapter 12. Awk Associative Arrays (associative array)


Guess you like

Origin www.cnblogs.com/petewell/p/11606882.html