Awk array iteration for multi-dimensional arrays

后端 未结 5 1292
不思量自难忘°
不思量自难忘° 2020-12-25 13:25

Awk offers associative indexing for array processing. Elements of 1 dimensional array can be iterated:

e.g.

for(index in arr1)
  print \"arr1[\" inde         


        
5条回答
  •  时光取名叫无心
    2020-12-25 14:11

    AWK fakes multidimensional arrays by concatenating the indices with the character held in the SUBSEP variable (0x1c). You can iterate through a two-dimensional array using split like this (based on an example in the info gawk file):

    awk 'BEGIN { OFS=","; array[1,2]=3; array[2,3]=5; array[3,4]=8; 
      for (comb in array) {split(comb,sep,SUBSEP);
        print sep[1], sep[2], array[sep[1],sep[2]]}}'
    

    Output:

    2,3,5
    3,4,8
    1,2,3
    

    You can, however, iterate over a numerically indexed array using nested for loops:

    for (i = 1; i <= width; i++)
        for (j = 1; j < = height; j++)
            print array[i, j]
    

    Another noteworthy bit of information from the GAWK manual:

    To test whether a particular index sequence exists in a multidimensional array, use the same operator (in) that is used for single dimensional arrays. Write the whole sequence of indices in parentheses, separated by commas, as the left operand:

         (subscript1, subscript2, ...) in array
    

    Gawk 4 adds arrays of arrays. From that link:

    for (i in array) {
        if (isarray(array[i])) {
            for (j in array[i]) {
                print array[i][j]
            }
        }
        else
            print array[i]
    }
    

    Also see Traversing Arrays of Arrays for information about the following function which walks an arbitrarily dimensioned array of arrays, including jagged ones:

    function walk_array(arr, name,      i)
    {
        for (i in arr) {
            if (isarray(arr[i]))
                walk_array(arr[i], (name "[" i "]"))
            else
                printf("%s[%s] = %s\n", name, i, arr[i])
        }
    } 
    

提交回复
热议问题