问题描述
我GOOGLE了它,并找出AWK 4.0后,我们可以通过把PROCINFO [sorted_in]命令前右循环打印定义的顺序数组。例如:
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]
在AWK 4.0.2,它的工作原理。不过,我想它在AWK 3.1.3的环境中,它没有工作。难道AWK的是早期的版本不支持这个功能呢?如何在AWK 3.1.3实现这一目标?
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?
推荐答案
只要保持第二阵列订单
带数字指标和第一个数组中的值的键。您可以通过序列订单
然后迭代,并查找阵列
的值:
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]]
}
在建订单
,你可能要检查钥匙是否已present在阵列
,以prevent 阵列
的按键被显示多次。
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.
这篇关于如何打印一个数组中定义顺序AWK 3.1.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!