gawk-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gawk-diffs] [SCM] gawk branch, master, updated. c2f829e9f960a10784d117b


From: Arnold Robbins
Subject: [gawk-diffs] [SCM] gawk branch, master, updated. c2f829e9f960a10784d117bf878e01b5eb18b1c7
Date: Sun, 08 May 2011 18:36:19 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gawk".

The branch, master has been updated
       via  c2f829e9f960a10784d117bf878e01b5eb18b1c7 (commit)
      from  7f813e5850978aedfce4b05c42255c8ffe8408a0 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.sv.gnu.org/cgit/gawk.git/commit/?id=c2f829e9f960a10784d117bf878e01b5eb18b1c7

commit c2f829e9f960a10784d117bf878e01b5eb18b1c7
Author: Arnold D. Robbins <address@hidden>
Date:   Sun May 8 21:36:04 2011 +0300

    More array sorting doc updates.

diff --git a/doc/gawk.info b/doc/gawk.info
index 4f0bcc0..3bdf268 100644
--- a/doc/gawk.info
+++ b/doc/gawk.info
@@ -13464,9 +13464,9 @@ File: gawk.info,  Node: Controlling Array Traversal,  
Next: Array Sorting Functi
 11.2.1 Controlling Array Traversal
 ----------------------------------
 
-By default, the order in which a `for (i in array)' loop will scan an
-array is not defined; it is generally based upon the internal
-implementation of arrays inside `awk'.
+By default, the order in which a `for (i in array)' loop scans an array
+is not defined; it is generally based upon the internal implementation
+of arrays inside `awk'.
 
    Often, though, it is desirable to be able to loop over the elements
 in a particular order that you, the programmer, choose.  `gawk' lets
@@ -13481,8 +13481,8 @@ you do this; this node describes how.
 
 File: gawk.info,  Node: Controlling Scanning With A Function,  Next: 
Controlling Scanning,  Up: Controlling Array Traversal
 
-11.2.1.1 Controlling Array Scanning Order With a User-defined Function
-......................................................................
+11.2.1.1 Array Scanning Using A User-defined Function
+.....................................................
 
 The value of `PROCINFO["sorted_in"]' can be a function name.  This lets
 you traverse an array based on any custom criterion.  The array
@@ -13500,23 +13500,25 @@ values of the two elements being compared.  Either V1 
or V2, or both,
 can be arrays if the array being traversed contains subarrays as
 values.  The three possible return values are interpreted this way:
 
-   * If the return value of `comp_func(i1, v1, i2, v2)' is less than
-     zero, index I1 comes before index I2 during loop traversal.
+`comp_func(i1, v1, i2, v2) < 0'
+     Index I1 comes before index I2 during loop traversal.
 
-   * If `comp_func(i1, v1, i2, v2)' returns zero, I1 and I2 come
-     together but the relative order with respect to each other is
-     undefined.
+`comp_func(i1, v1, i2, v2) == 0'
+     Indices I1 and I2 come together but the relative order with
+     respect to each other is undefined.
 
-   * If the return value of `comp_func(i1, v1, i2, v2)' is greater than
-     zero, I1 comes after I2.
+`'
+
+`comp_func(i1, v1, i2, v2) > 0'
+     Index I1 comes after index I2 during loop traversal.
 
    The following comparison function can be used to scan an array in
 numerical order of the indices:
 
      function cmp_num_idx(i1, v1, i2, v2)
      {
-         # numerical index comparison, ascending order
-         return (i1 - i2)
+          # numerical index comparison, ascending order
+          return (i1 - i2)
      }
 
    This function traverses an array based on the string order of the
@@ -13538,22 +13540,65 @@ loop traversal:
 
      function cmp_num_str_val(i1, v1, i2, v2,   n1, n2)
      {
-         # numbers before string value comparison, ascending order
-         n1 = v1 + 0
-         n2 = v2 + 0
-         if (n1 == v1)
-             return (n2 == v2) ? (n1 - n2) : -1
-         else if (n2 == v2)
-             return 1
-         return (v1 < v2) ? -1 : (v1 != v2)
+          # numbers before string value comparison, ascending order
+          n1 = v1 + 0
+          n2 = v2 + 0
+          if (n1 == v1)
+              return (n2 == v2) ? (n1 - n2) : -1
+          else if (n2 == v2)
+              return 1
+          return (v1 < v2) ? -1 : (v1 != v2)
      }
 
-   *FIXME*: Put in a fuller example here of some data and show the
-different results when traversing.
+   Here is a main program to demonstrate how `gawk' behaves using each
+of the previous functions:
+
+     BEGIN {
+         data["one"] = 10
+         data["two"] = 20
+         data[10] = "one"
+         data[100] = 100
+         data[20] = "two"
+
+         f[1] = "cmp_num_idx"
+         f[2] = "cmp_str_val"
+         f[3] = "cmp_num_str_val"
+         for (i = 1; i <= 3; i++) {
+             printf("Sort function: %s\n", f[i])
+             PROCINFO["sorted_in"] = f[i]
+             for (j in data)
+                 printf("\tdata[%s] = %s\n", j, data[j])
+             print ""
+         }
+     }
+
+   Here are the results when the program is run:
+
+     $ gawk -f compdemo.awk
+     -| Sort function: cmp_num_idx      Sort by numeric index
+     -|        data[two] = 20
+     -|        data[one] = 10           Both strings are numerically zero
+     -|        data[10] = one
+     -|        data[20] = two
+     -|        data[100] = 100
+     -|
+     -| Sort function: cmp_str_val      Compare values as strings
+     -|        data[one] = 10
+     -|        data[100] = 100          String 100 is less than string 20
+     -|        data[two] = 20
+     -|        data[10] = one
+     -|        data[20] = two
+     -|
+     -| Sort function: cmp_num_str_val  All numbers before all strings
+     -|        data[one] = 10
+     -|        data[two] = 20
+     -|        data[100] = 100
+     -|        data[10] = one
+     -|        data[20] = two
 
    Consider sorting the entries of a GNU/Linux system password file
-according to login names.  The following program which sorts records by
-a specific field position can be used for this purpose:
+according to login names.  The following program sorts records by a
+specific field position and can be used for this purpose:
 
      # sort.awk --- simple program to sort by field position
      # field position is specified by the global variable POS
@@ -13582,7 +13627,7 @@ a specific field position can be used for this purpose:
 
    The first field in each entry of the password file is the user's
 login name, and the fields are seperated by colons.  Each record
-defines a subarray, which each field as an element in the subarray.
+defines a subarray, with each field as an element in the subarray.
 Running the program produces the following output:
 
      $ gawk -vPOS=1 -F: -f sort.awk /etc/passwd
@@ -13695,9 +13740,12 @@ real `awk' function names.
      compared as strings.  Subarrays, if present, come out last.
 
 `"@val_num_asc"'
-     Order by values but force scalar values to be treated as numbers
-     for the purpose of comparison.  If there are subarrays, those
-     appear at the end of the sorted list.
+     Order by element values but force scalar values to be treated as
+     numbers for the purpose of comparison.  If there are subarrays,
+     those appear at the end of the sorted list.  When numeric values
+     are equal, the string values are used to provide an ordering: this
+     guarantees consistent results across different operating systems
+     and/or library versions of the C `qsort()' function.
 
 `"@ind_str_desc"'
      Reverse order from the most basic sort.
@@ -13790,21 +13838,11 @@ and then sorts `dest', destroying its indices.  
However, the `source'
 array is not affected.
 
    `asort()' and `asorti()' accept a third string argument to control
-the comparison rule for the array elements, and the direction of the
-sorted results.  The valid comparison modes are `string' and `number',
-and the direction can be either `ascending' or `descending'.  Either
-mode or direction, or both, can be omitted in which case the defaults,
-`string' or `ascending' is assumed for the comparison mode and the
-direction, respectively.  Seperate comparison mode from direction with
-a single space, and they can appear in any order.  To compare the
-elements as numbers, and to reverse the elements of the `dest' array,
-the call to asort in the above example can be replaced with:
-
-     asort(source, dest, "descending number")
-
-   The third argument to `asort()' can also be a user-defined function
-name which is used to order the array elements before constructing the
-result array.  *Note Scanning an Array::, for more information.
+comparison of array elements.
+
+   As with `PROCINFO["sorted_in"]', this argument may be the name of a
+user-defined function, or one of the predefined names that `gawk'
+provides (*note Controlling Scanning With A Function::).
 
    Often, what's needed is to sort on the values of the _indices_
 instead of the values of the elements.  To do that, use the `asorti()'
@@ -13828,8 +13866,7 @@ result array:
    Sorting the array by replacing the indices provides maximal
 flexibility.  To traverse the elements in decreasing order, use a loop
 that goes from N down to 1, either over the elements or over the
-indices.  This is an alternative to specifying `descending' for the
-sorting order using the optional third argument.
+indices.
 
    Copying array indices and elements isn't expensive in terms of
 memory.  Internally, `gawk' maintains "reference counts" to data.  For
@@ -24843,7 +24880,7 @@ Index
 * arrays, sorting:                       Array Sorting Functions.
                                                               (line   6)
 * arrays, sorting, IGNORECASE variable and: Array Sorting Functions.
-                                                              (line  85)
+                                                              (line  74)
 * arrays, sparse:                        Array Intro.         (line  71)
 * arrays, subscripts:                    Numeric Array Subscripts.
                                                               (line   6)
@@ -25888,7 +25925,7 @@ Index
 * gawk, functions, adding:               Dynamic Extensions.  (line  10)
 * gawk, hexadecimal numbers and:         Nondecimal-numbers.  (line  42)
 * gawk, IGNORECASE variable in <1>:      Array Sorting Functions.
-                                                              (line  85)
+                                                              (line  74)
 * gawk, IGNORECASE variable in <2>:      String Functions.    (line  29)
 * gawk, IGNORECASE variable in <3>:      Array Intro.         (line  92)
 * gawk, IGNORECASE variable in <4>:      User-modified.       (line  82)
@@ -26048,13 +26085,13 @@ Index
 * igawk.sh program:                      Igawk Program.       (line 124)
 * ignore debugger command:               Breakpoint Control.  (line  86)
 * IGNORECASE variable <1>:               Array Sorting Functions.
-                                                              (line  85)
+                                                              (line  74)
 * IGNORECASE variable <2>:               String Functions.    (line  29)
 * IGNORECASE variable <3>:               Array Intro.         (line  92)
 * IGNORECASE variable <4>:               User-modified.       (line  82)
 * IGNORECASE variable:                   Case-sensitivity.    (line  26)
 * IGNORECASE variable, array sorting and: Array Sorting Functions.
-                                                              (line  85)
+                                                              (line  74)
 * IGNORECASE variable, array subscripts and: Array Intro.     (line  92)
 * IGNORECASE variable, in example programs: Library Functions.
                                                               (line  42)
@@ -26736,7 +26773,7 @@ Index
 * redirection of input:                  Getline/File.        (line   6)
 * redirection of output:                 Redirection.         (line   6)
 * reference counting, sorting arrays:    Array Sorting Functions.
-                                                              (line  79)
+                                                              (line  68)
 * regexp constants <1>:                  Comparison Operators.
                                                               (line 103)
 * regexp constants <2>:                  Regexp Constants.    (line   6)
@@ -27534,152 +27571,152 @@ Node: Advanced Features554205
 Node: Nondecimal Data555718
 Node: Array Sorting557301
 Node: Controlling Array Traversal558001
-Node: Controlling Scanning With A Function558752
-Node: Controlling Scanning565232
-Node: Array Sorting Functions568860
-Ref: Array Sorting Functions-Footnote-1572854
-Node: Two-way I/O573048
-Ref: Two-way I/O-Footnote-1578480
-Node: TCP/IP Networking578550
-Node: Profiling581394
-Node: Library Functions588868
-Ref: Library Functions-Footnote-1591875
-Node: Library Names592046
-Ref: Library Names-Footnote-1595517
-Ref: Library Names-Footnote-2595737
-Node: General Functions595823
-Node: Strtonum Function596776
-Node: Assert Function599706
-Node: Round Function603032
-Node: Cliff Random Function604575
-Node: Ordinal Functions605591
-Ref: Ordinal Functions-Footnote-1608661
-Ref: Ordinal Functions-Footnote-2608913
-Node: Join Function609122
-Ref: Join Function-Footnote-1610893
-Node: Gettimeofday Function611093
-Node: Data File Management614808
-Node: Filetrans Function615440
-Node: Rewind Function619579
-Node: File Checking620966
-Node: Empty Files622060
-Node: Ignoring Assigns624290
-Node: Getopt Function625843
-Ref: Getopt Function-Footnote-1637147
-Node: Passwd Functions637350
-Ref: Passwd Functions-Footnote-1646325
-Node: Group Functions646413
-Node: Walking Arrays654497
-Node: Sample Programs656066
-Node: Running Examples656731
-Node: Clones657459
-Node: Cut Program658683
-Node: Egrep Program668528
-Ref: Egrep Program-Footnote-1676301
-Node: Id Program676411
-Node: Split Program680027
-Ref: Split Program-Footnote-1683546
-Node: Tee Program683674
-Node: Uniq Program686477
-Node: Wc Program693906
-Ref: Wc Program-Footnote-1698172
-Ref: Wc Program-Footnote-2698372
-Node: Miscellaneous Programs698464
-Node: Dupword Program699652
-Node: Alarm Program701683
-Node: Translate Program706432
-Ref: Translate Program-Footnote-1710819
-Ref: Translate Program-Footnote-2711047
-Node: Labels Program711181
-Ref: Labels Program-Footnote-1714552
-Node: Word Sorting714636
-Node: History Sorting718520
-Node: Extract Program720359
-Ref: Extract Program-Footnote-1727842
-Node: Simple Sed727970
-Node: Igawk Program731032
-Ref: Igawk Program-Footnote-1746065
-Ref: Igawk Program-Footnote-2746266
-Node: Anagram Program746404
-Node: Signature Program749472
-Node: Debugger750572
-Node: Debugging751483
-Node: Debugging Concepts751896
-Node: Debugging Terms753752
-Node: Awk Debugging756374
-Node: Sample dgawk session757266
-Node: dgawk invocation757758
-Node: Finding The Bug758940
-Node: List of Debugger Commands765426
-Node: Breakpoint Control766737
-Node: Dgawk Execution Control770373
-Node: Viewing And Changing Data773724
-Node: Dgawk Stack777061
-Node: Dgawk Info778521
-Node: Miscellaneous Dgawk Commands782469
-Node: Readline Support787897
-Node: Dgawk Limitations788735
-Node: Language History790924
-Node: V7/SVR3.1792362
-Node: SVR4794683
-Node: POSIX796125
-Node: BTL797133
-Node: POSIX/GNU797867
-Node: Common Extensions803018
-Node: Contributors804119
-Node: Installation808295
-Node: Gawk Distribution809189
-Node: Getting809673
-Node: Extracting810499
-Node: Distribution contents812191
-Node: Unix Installation817413
-Node: Quick Installation818030
-Node: Additional Configuration Options819992
-Node: Configuration Philosophy821469
-Node: Non-Unix Installation823811
-Node: PC Installation824269
-Node: PC Binary Installation825568
-Node: PC Compiling827416
-Node: PC Testing830360
-Node: PC Using831536
-Node: Cygwin835721
-Node: MSYS836721
-Node: VMS Installation837235
-Node: VMS Compilation837838
-Ref: VMS Compilation-Footnote-1838845
-Node: VMS Installation Details838903
-Node: VMS Running840538
-Node: VMS Old Gawk842145
-Node: Bugs842619
-Node: Other Versions846529
-Node: Notes851808
-Node: Compatibility Mode852500
-Node: Additions853283
-Node: Accessing The Source854095
-Node: Adding Code855520
-Node: New Ports861487
-Node: Dynamic Extensions865600
-Node: Internals866976
-Node: Plugin License876079
-Node: Sample Library876713
-Node: Internal File Description877399
-Node: Internal File Ops881114
-Ref: Internal File Ops-Footnote-1885895
-Node: Using Internal File Ops886035
-Node: Future Extensions888412
-Node: Basic Concepts890916
-Node: Basic High Level891673
-Ref: Basic High Level-Footnote-1895708
-Node: Basic Data Typing895893
-Node: Floating Point Issues900418
-Node: String Conversion Precision901501
-Ref: String Conversion Precision-Footnote-1903201
-Node: Unexpected Results903310
-Node: POSIX Floating Point Problems905136
-Ref: POSIX Floating Point Problems-Footnote-1908841
-Node: Glossary908879
-Node: Copying933855
-Node: GNU Free Documentation License971412
-Node: Index996549
+Node: Controlling Scanning With A Function558748
+Node: Controlling Scanning566372
+Node: Array Sorting Functions570229
+Ref: Array Sorting Functions-Footnote-1573471
+Node: Two-way I/O573665
+Ref: Two-way I/O-Footnote-1579097
+Node: TCP/IP Networking579167
+Node: Profiling582011
+Node: Library Functions589485
+Ref: Library Functions-Footnote-1592492
+Node: Library Names592663
+Ref: Library Names-Footnote-1596134
+Ref: Library Names-Footnote-2596354
+Node: General Functions596440
+Node: Strtonum Function597393
+Node: Assert Function600323
+Node: Round Function603649
+Node: Cliff Random Function605192
+Node: Ordinal Functions606208
+Ref: Ordinal Functions-Footnote-1609278
+Ref: Ordinal Functions-Footnote-2609530
+Node: Join Function609739
+Ref: Join Function-Footnote-1611510
+Node: Gettimeofday Function611710
+Node: Data File Management615425
+Node: Filetrans Function616057
+Node: Rewind Function620196
+Node: File Checking621583
+Node: Empty Files622677
+Node: Ignoring Assigns624907
+Node: Getopt Function626460
+Ref: Getopt Function-Footnote-1637764
+Node: Passwd Functions637967
+Ref: Passwd Functions-Footnote-1646942
+Node: Group Functions647030
+Node: Walking Arrays655114
+Node: Sample Programs656683
+Node: Running Examples657348
+Node: Clones658076
+Node: Cut Program659300
+Node: Egrep Program669145
+Ref: Egrep Program-Footnote-1676918
+Node: Id Program677028
+Node: Split Program680644
+Ref: Split Program-Footnote-1684163
+Node: Tee Program684291
+Node: Uniq Program687094
+Node: Wc Program694523
+Ref: Wc Program-Footnote-1698789
+Ref: Wc Program-Footnote-2698989
+Node: Miscellaneous Programs699081
+Node: Dupword Program700269
+Node: Alarm Program702300
+Node: Translate Program707049
+Ref: Translate Program-Footnote-1711436
+Ref: Translate Program-Footnote-2711664
+Node: Labels Program711798
+Ref: Labels Program-Footnote-1715169
+Node: Word Sorting715253
+Node: History Sorting719137
+Node: Extract Program720976
+Ref: Extract Program-Footnote-1728459
+Node: Simple Sed728587
+Node: Igawk Program731649
+Ref: Igawk Program-Footnote-1746682
+Ref: Igawk Program-Footnote-2746883
+Node: Anagram Program747021
+Node: Signature Program750089
+Node: Debugger751189
+Node: Debugging752100
+Node: Debugging Concepts752513
+Node: Debugging Terms754369
+Node: Awk Debugging756991
+Node: Sample dgawk session757883
+Node: dgawk invocation758375
+Node: Finding The Bug759557
+Node: List of Debugger Commands766043
+Node: Breakpoint Control767354
+Node: Dgawk Execution Control770990
+Node: Viewing And Changing Data774341
+Node: Dgawk Stack777678
+Node: Dgawk Info779138
+Node: Miscellaneous Dgawk Commands783086
+Node: Readline Support788514
+Node: Dgawk Limitations789352
+Node: Language History791541
+Node: V7/SVR3.1792979
+Node: SVR4795300
+Node: POSIX796742
+Node: BTL797750
+Node: POSIX/GNU798484
+Node: Common Extensions803635
+Node: Contributors804736
+Node: Installation808912
+Node: Gawk Distribution809806
+Node: Getting810290
+Node: Extracting811116
+Node: Distribution contents812808
+Node: Unix Installation818030
+Node: Quick Installation818647
+Node: Additional Configuration Options820609
+Node: Configuration Philosophy822086
+Node: Non-Unix Installation824428
+Node: PC Installation824886
+Node: PC Binary Installation826185
+Node: PC Compiling828033
+Node: PC Testing830977
+Node: PC Using832153
+Node: Cygwin836338
+Node: MSYS837338
+Node: VMS Installation837852
+Node: VMS Compilation838455
+Ref: VMS Compilation-Footnote-1839462
+Node: VMS Installation Details839520
+Node: VMS Running841155
+Node: VMS Old Gawk842762
+Node: Bugs843236
+Node: Other Versions847146
+Node: Notes852425
+Node: Compatibility Mode853117
+Node: Additions853900
+Node: Accessing The Source854712
+Node: Adding Code856137
+Node: New Ports862104
+Node: Dynamic Extensions866217
+Node: Internals867593
+Node: Plugin License876696
+Node: Sample Library877330
+Node: Internal File Description878016
+Node: Internal File Ops881731
+Ref: Internal File Ops-Footnote-1886512
+Node: Using Internal File Ops886652
+Node: Future Extensions889029
+Node: Basic Concepts891533
+Node: Basic High Level892290
+Ref: Basic High Level-Footnote-1896325
+Node: Basic Data Typing896510
+Node: Floating Point Issues901035
+Node: String Conversion Precision902118
+Ref: String Conversion Precision-Footnote-1903818
+Node: Unexpected Results903927
+Node: POSIX Floating Point Problems905753
+Ref: POSIX Floating Point Problems-Footnote-1909458
+Node: Glossary909496
+Node: Copying934472
+Node: GNU Free Documentation License972029
+Node: Index997166
 
 End Tag Table
diff --git a/doc/gawk.texi b/doc/gawk.texi
index a1f709c..d594639 100644
--- a/doc/gawk.texi
+++ b/doc/gawk.texi
@@ -18220,7 +18220,7 @@ to order the elements during sorting.
 @subsection Controlling Array Traversal
 
 By default, the order in which a @samp{for (i in array)} loop
-will scan an array is not defined; it is generally based upon
+scans an array is not defined; it is generally based upon
 the internal implementation of arrays inside @command{awk}.
 
 Often, though, it is desirable to be able to loop over the elements
@@ -18234,7 +18234,7 @@ lets you do this; this @value{SUBSECTION} describes how.
 @end menu
 
 @node Controlling Scanning With A Function
address@hidden Controlling Array Scanning Order With a User-defined Function
address@hidden Array Scanning Using A User-defined Function
 
 The value of @code{PROCINFO["sorted_in"]} can be a function name.
 This lets you traverse an array based on any custom criterion.
@@ -18256,19 +18256,18 @@ Either @var{v1} or @var{v2}, or both, can be arrays 
if the array being
 traversed contains subarrays as values.  The three possible return values
 are interpreted this way:
 
address@hidden @bullet
address@hidden
-If the return value of @code{comp_func(i1, v1, i2, v2)} is less than zero,
-index @var{i1} comes before index @var{i2} during loop traversal.
address@hidden @code
address@hidden comp_func(i1, v1, i2, v2) < 0
+Index @var{i1} comes before index @var{i2} during loop traversal.
 
address@hidden
-If @code{comp_func(i1, v1, i2, v2)} returns zero, @var{i1} and @var{i2}
address@hidden comp_func(i1, v1, i2, v2) == 0
+Indices @var{i1} and @var{i2}
 come together but the relative order with respect to each other is undefined.
 
 @item
-If the return value of @code{comp_func(i1, v1, i2, v2)} is greater than zero,
address@hidden comes after @var{i2}.
address@hidden itemize
address@hidden comp_func(i1, v1, i2, v2) > 0
+Index @var{i1} comes after index @var{i2} during loop traversal.
address@hidden table
 
 The following comparison function can be used to scan an array in
 numerical order of the indices:
@@ -18276,8 +18275,8 @@ numerical order of the indices:
 @example
 function cmp_num_idx(i1, v1, i2, v2)
 @{
-    # numerical index comparison, ascending order
-    return (i1 - i2)
+     # numerical index comparison, ascending order
+     return (i1 - i2)
 @}
 @end example
 
@@ -18303,23 +18302,71 @@ any leading or trailing spaces, come out first during 
loop traversal:
 @example
 function cmp_num_str_val(i1, v1, i2, v2,   n1, n2)
 @{
-    # numbers before string value comparison, ascending order
-    n1 = v1 + 0
-    n2 = v2 + 0
-    if (n1 == v1) 
-        return (n2 == v2) ? (n1 - n2) : -1
-    else if (n2 == v2)
-        return 1 
-    return (v1 < v2) ? -1 : (v1 != v2)
+     # numbers before string value comparison, ascending order
+     n1 = v1 + 0
+     n2 = v2 + 0
+     if (n1 == v1) 
+         return (n2 == v2) ? (n1 - n2) : -1
+     else if (n2 == v2)
+         return 1 
+     return (v1 < v2) ? -1 : (v1 != v2)
address@hidden
address@hidden example
+
+Here is a main program to demonstrate how @command{gawk}
+behaves using each of the previous functions:
+
address@hidden
+BEGIN @{
+    data["one"] = 10
+    data["two"] = 20
+    data[10] = "one"
+    data[100] = 100
+    data[20] = "two"
+    
+    f[1] = "cmp_num_idx"
+    f[2] = "cmp_str_val"
+    f[3] = "cmp_num_str_val"
+    for (i = 1; i <= 3; i++) @{
+        printf("Sort function: %s\n", f[i])
+        PROCINFO["sorted_in"] = f[i]
+        for (j in data)
+            printf("\tdata[%s] = %s\n", j, data[j])
+        print ""
+    @}
 @}
 @end example
 
address@hidden: Put in a fuller example here of some data
-and show the different results when traversing.
+Here are the results when the program is run:
address@hidden
+
address@hidden
+$ @kbd{gawk -f compdemo.awk}
address@hidden Sort function: cmp_num_idx      @ii{Sort by numeric index}
address@hidden     data[two] = 20
address@hidden     data[one] = 10              @ii{Both strings are numerically 
zero}
address@hidden     data[10] = one
address@hidden     data[20] = two
address@hidden     data[100] = 100
address@hidden 
address@hidden Sort function: cmp_str_val      @ii{Compare values as strings}
address@hidden     data[one] = 10
address@hidden     data[100] = 100             @ii{String 100 is less than 
string 20}
address@hidden     data[two] = 20
address@hidden     data[10] = one
address@hidden     data[20] = two
address@hidden 
address@hidden Sort function: cmp_num_str_val  @ii{All numbers before all 
strings}
address@hidden     data[one] = 10
address@hidden     data[two] = 20
address@hidden     data[100] = 100
address@hidden     data[10] = one
address@hidden     data[20] = two
address@hidden example
 
 Consider sorting the entries of a GNU/Linux system password file
-according to login names.  The following program which sorts records
-by a specific field position can be used for this purpose:   
+according to login names.  The following program sorts records
+by a specific field position and can be used for this purpose:   
 
 @example
 # sort.awk --- simple program to sort by field position
@@ -18350,7 +18397,7 @@ END @{
 
 The first field in each entry of the password file is the user's login name,
 and the fields are seperated by colons.
-Each record defines a subarray, which each field as an element in the subarray.
+Each record defines a subarray, with each field as an element in the subarray.
 Running the program produces the
 following output:
 
@@ -18488,9 +18535,13 @@ Order by element values rather than by indices.  
Scalar values are
 compared as strings.  Subarrays, if present, come out last.
 
 @item "@@val_num_asc"
-Order by values but force scalar values to be treated as numbers
+Order by element values but force scalar values to be treated as numbers
 for the purpose of comparison.  If there are subarrays, those appear
 at the end of the sorted list.
+When numeric values are equal, the string values are used to provide
+an ordering: this guarantees consistent results across different
+operating systems and/or library versions of the C @code{qsort()}
+function.
 
 @item "@@ind_str_desc"
 Reverse order from the most basic sort.
@@ -18502,18 +18553,18 @@ Numeric indices ordered from high to low.
 Element values, based on type, in descending order.
 
 @item "@@val_str_desc"
-Element values, treated as strings, ordered from high to low.  Subarrays, if 
present,
-come out first.
+Element values, treated as strings, ordered from high to low.
+Subarrays, if present, come out first.
 
 @item "@@val_num_desc"
-Element values, treated as numbers, ordered from high to low.  Subarrays, if 
present,
-come out first.
+Element values, treated as numbers, ordered from high to low.
+Subarrays, if present, come out first.
 
 @item "@@unsorted"
-Array elements are processed in arbitrary order, which is the normal 
@command{awk}
-behavior. You can also get the normal behavior by just
-deleting the @code{"sorted_in"} element from the @code{PROCINFO} array, if
-it previously had a value assigned to it.
+Array elements are processed in arbitrary order, which is the normal
address@hidden behavior. You can also get the normal behavior by just
+deleting the @code{"sorted_in"} element from the @code{PROCINFO} array,
+if it previously had a value assigned to it.
 @end table
 
 The array traversal order is determined before the @code{for} loop
@@ -18597,26 +18648,12 @@ In this case, @command{gawk} copies the @code{source} 
array into the
 However, the @code{source} array is not affected.
 
 @code{asort()} and @code{asorti()} accept a third string argument
-to control the comparison rule for the array elements, and the direction
-of the sorted results.  The valid comparison modes are @samp{string} and 
@samp{number},
-and the direction can be either @samp{ascending} or @samp{descending}.   
-Either mode or direction, or both, can be omitted in which
-case the defaults, @samp{string} or @samp{ascending} is assumed
-for the comparison mode and the direction, respectively.  Seperate comparison
-mode from direction with a single space, and they can appear in any
-order.  To compare the elements as numbers, and to reverse the elements
-of the @code{dest} array, the call to asort in the above example can be
-replaced with:
-
address@hidden
-asort(source, dest, "descending number")
address@hidden example
+to control comparison of array elements.
 
-The third argument to @code{asort()} can also be a user-defined
-function name which is used to order the array elements before
-constructing the result array.
address@hidden an Array}, for more information.
- 
+As with @code{PROCINFO["sorted_in"]}, this argument may be the
+name of a user-defined function, or one of the predefined names
+that @command{gawk} provides
+(@pxref{Controlling Scanning With A Function}).
 
 Often, what's needed is to sort on the values of the @emph{indices}
 instead of the values of the elements.
@@ -18642,9 +18679,7 @@ END @{
 
 Sorting the array by replacing the indices provides maximal flexibility.
 To traverse the elements in decreasing order, use a loop that goes from
address@hidden down to 1, either over the elements or over the indices.  This
-is an alternative to specifying @samp{descending} for the sorting order
-using the optional third argument.
address@hidden down to 1, either over the elements or over the indices.
 
 @cindex reference counting, sorting arrays
 Copying array indices and elements isn't expensive in terms of memory.

-----------------------------------------------------------------------

Summary of changes:
 doc/gawk.info |  435 +++++++++++++++++++++++++++++++--------------------------
 doc/gawk.texi |  149 ++++++++++++--------
 2 files changed, 328 insertions(+), 256 deletions(-)


hooks/post-receive
-- 
gawk



reply via email to

[Prev in Thread] Current Thread [Next in Thread]