manpagez: man pages & more
info gawk
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

11.2.1.2 Controlling Array Scanning Order

As described in Array Scanning Using A User-defined Function, you can provide the name of a function as the value of PROCINFO["sorted_in"] to specify custom sorting criteria.

Often, though, you may wish to do something simple, such as “sort based on comparing the indices in ascending order,” or “sort based on comparing the values in descending order.” Having to write a simple comparison function for this purpose for use in all of your programs becomes tedious. For the common simple cases, gawk provides the option of supplying special names that do the requested sorting for you. You can think of them as “predefined” sorting functions, if you like, although the names purposely include characters that are not valid in real awk function names.

The following special values are available:

"@ind_str_asc"

Order by indices compared as strings; this is the most basic sort. (Internally, array indices are always strings, so with ‘a[2*5] = 1’ the index is "10" rather than numeric 10.)

"@ind_num_asc"

Order by indices but force them to be treated as numbers in the process. Any index with a non-numeric value will end up positioned as if it were zero.

"@val_type_asc"

Order by element values rather than indices. Ordering is by the type assigned to the element (see section Variable Typing and Comparison Expressions). All numeric values come before all string values, which in turn come before all subarrays.

"@val_str_asc"

Order by element values rather than by indices. Scalar values are compared as strings. Subarrays, if present, come out last.

"@val_num_asc"

Order by element values rather than by indices. Scalar values are compared as numbers. Subarrays, if present, come out last. When numeric values are equal, the string values are used to provide an ordering: this guarantees consistent results across different versions of the C qsort() function.(59)

"@ind_str_desc"

Reverse order from the most basic sort.

"@ind_num_desc"

Numeric indices ordered from high to low.

"@val_type_desc"

Element values, based on type, in descending order.

"@val_str_desc"

Element values, treated as strings, ordered from high to low. Subarrays, if present, come out first.

"@val_num_desc"

Element values, treated as numbers, ordered from high to low. Subarrays, if present, come out first.

"@unsorted"

Array elements are processed in arbitrary order, which is the normal awk behavior. You can also get the normal behavior by just deleting the "sorted_in" element from the PROCINFO array, if it previously had a value assigned to it.

The array traversal order is determined before the for loop starts to run. Changing PROCINFO["sorted_in"] in the loop body will not affect the loop.

For example:

 
$ gawk 'BEGIN {
>    a[4] = 4
>    a[3] = 3
>    for (i in a)
>        print i, a[i]
> }'
-| 4 4
-| 3 3
$ gawk 'BEGIN {
>    PROCINFO["sorted_in"] = "@str_ind_asc"
>    a[4] = 4
>    a[3] = 3
>    for (i in a)
>        print i, a[i]
> }'
-| 3 3
-| 4 4

When sorting an array by element values, if a value happens to be a subarray then it is considered to be greater than any string or numeric value, regardless of what the subarray itself contains, and all subarrays are treated as being equal to each other. Their order relative to each other is determined by their index strings.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2025
Individual documents may contain additional copyright information.