Arrays 3
Other Ways to Display Arrays
We've seen 2 ways to sort an array using asort, asorti but there are several more ways to sort an array using predefined array scanning orders.
Using the array from the previous page:
BEGIN {
print "Using PROCINFO ...\n"
awry["three"] = "blueberry"
awry["two"] = "cherry"
awry["seven"] = "pineapple"
awry["eight"] = "orange"
awry["five"] = "grape"
awry["six"] = "strawberry"
awry["four"] = "apple"
awry["1"] = "pear"
PROCINFO["sorted_in"] = "@ind_str_asc"
print "\nSorted by index in ascending order ...";
for (i in awry) {
print i " : " awry[i]
}
This is the same as the previous method.
The key line is
PROCINFO["sorted_in"] = "@ind_str_asc"
@ind_str_asc is the argument to the built-in variable PROCINFO
Think of that as index string ascending
Here is a list of all the 'arguments' you can use with PROCINFO:
- @unsorted
elements are processed in arbitrary order
- @ind_str_asc
ascending order compared as strings
- @ind_num_asc
ascending order but force them to be treated as numbers
- @val_type_asc
order values in ascending order (rather than by indices).
Ordering is by the type assigned to the element
Subarrays come out last
- @val_str_asc
order element values in ascending order (rather than by indices)
Scalar values are compared as strings.
Subarrays, if present, come out last.
- @val_num_asc
order element values in ascending order (rather than by indices).
Scalar values are compared as numbers.
Subarrays, if present, come out last.
- @ind_str_desc
string indices are ordered from high to low
- @ind_num_desc
numeric indices are ordered from high to low
- @val_type_desc
element values, based on type, are ordered from high to low
Subarrays, if present, come out first.
- @val_str_desc
element values, treated as strings, are ordered from high to low
Subarrays, if present, come out first.
- @val_num_desc
element values, treated as numbers, are ordered from high to low
Subarrays, if present, come out first.
All numeric values come before all string values
That is a LOT of control available!
Observant readers will also want to know what subarrays are.