- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2014-10-03 13:09 编辑
回复 1# wy200747055
http://www.gnu.org/software/gawk ... ontrolling-Scanning
8.1.5 Scanning All Elements of an Array
...
8.1.6 Using Predefined Array Scanning Orders
By default, when a for loop traverses an array, the order is undefined, meaning that the awk implementation determines the order in which the array is traversed. This order is usually based on the internal implementation of arrays and will vary from one version of awk to the next.
Often, though, you may wish to do something simple, such as “traverse the array by comparing the indices in ascending order,” or “traverse the array by comparing the values in descending order.” gawk provides two mechanisms which give you this control.
Set PROCINFO["sorted_in"] to one of a set of predefined values. We describe this now.
Set PROCINFO["sorted_in"] to the name of a user-defined function to use for comparison of array elements. This advanced feature is described later, in Array Sorting.
The following special values for PROCINFO["sorted_in"] are available:
"@unsorted"
Array elements are processed in arbitrary order, which is the default awk behavior.
"@ind_str_asc"
Order by indices in ascending order 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 in ascending order 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 in ascending order (rather than by indices). Ordering is by the type assigned to the element (see Typing and Comparison). All numeric values come before all string values, which in turn come before all subarrays. (Subarrays have not been described yet; see Arrays of Arrays.)
"@val_str_asc"
Order by 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 by element values in ascending order (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,41 which gawk uses internally to perform the sorting.
....
|
|