How to print an array in a specific order in AWK 3.1.3

I looked through it and found that after AWK 4.0 we can print the array in a specific order by placing the PROCINFO ["sorted_in"] command right before the loop. for instance

    PROCINFO["sorted_in"] = "@ind_num_asc"
    for( i in array)
          print i, array[i]

In AWK 4.0.2, it works. However, I tried this in AWK 3.1.3, this did not work. Doesn't this early version of AWK support this feature? How to achieve this goal in AWK 3.1.3?

+5
source share
1 answer

Just save the second array orderwith numeric indices and keys for the first array as values. Then you can step through orderand look for values array:

for (i = 1; i < length(order); i++) {
  print order[i], array[order[i]]
}

order , array, array.

+4

All Articles