How to print an array in defined order in AWK 3.1.3
awk
Solution
Just keep a second array `order` with numerical indices and the keys for the first array as the values. You can then iterate through `order` in sequence and look up the values of `array`:
for (i = 1; i < length(order); i++) {
print order[i], array[order[i]]
}
When building `order`, you may want to check whether the key is already present in `array`, to prevent the keys of `array` being shown multiple times.
Problem
I googled it and find out that after AWK 4.0 we can print an array in defined order by putting PROCINFO["sorted_in"] command right before for loop. For example ``` PROCINFO["sorted_in"] = "@ind_num_asc" for( i in array) print i, array[i] ``` In AWK 4.0.2, it works. However, I tried it in AWK 3.1.3 environment, it did not work. Does this early version of AWK do not support this function? How to achieve this goal in AWK 3.1.3?