Why does awk seem to randomize the array?

前端 未结 4 830
忘了有多久
忘了有多久 2020-11-29 12:36

If you look at output of this awk test, you see that array in awk seems to be printed at some random pattern. It seems to be in same o

4条回答
  •  情话喂你
    2020-11-29 13:18

    The issue is the operator you use to get the array indices, not the fact that the array is stored in a hash table.

    The in operator provides the array indices in a random(-looking) order (which IS by default related to the hash table but that's an implementation choice and can be modified).

    A for loop that explicitly provides the array indices in a numerically increasing order also operates on the same hash table that the in operator on but that produces output in a specific order regardless.

    It's just 2 different ways of getting the array indices, both of which work on a hash table.

    man awk and look up the in operator.

    If you want to control the output order using the in operator, you can do so with GNU awk (from release 4.0 on) by populating PROCINFO["sorted_in"]. See http://www.gnu.org/software/gawk/manual/gawk.html#Controlling-Array-Traversal for details.

    Some common ways to access array indices:

    To print array elements in an order you don't care about:

    {a[$1]=$0} END{for (i in a) print i, a[i]}
    

    To print array elements in numeric order of indices if the indices are numeric and contiguous starting at 1:

    {a[++i]=$0} END{for (i=1;i in a;i++) print i, a[i]}
    

    To print array elements in numeric order of indices if the indices are numeric but non-contiguous:

    {a[$1]=$0; min=($1max?$1:max)} END{for (i=min;i<=max;i++) if (i in a) print i, a[i]}
    

    To print array elements in the order they were seen in the input:

    {a[$1]=$0; b[++max]=$1} END{for (i=1;i <= max;i++) print b[i], a[b[i]]}
    

    To print array elements in a specific order of indices using gawk 4.0+:

    BEGIN{PROCINFO["sorted_in"]=whatever} {a[$1]=$0} END{for (i in a) print i, a[i]}
    

    For anything else, write your own code and/or see gawk asort() and asorti().

提交回复
热议问题