[Top][All Lists]

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

asorti() comparison

Subject: asorti() comparison
Date: Tue, 19 Sep 2006 08:05:44 +0200


I use GNU Awk 3.1.5. My system type is sparc-sun-solaris2.8.

I run gawk -f foo.awk and i get the result `ajbcdefghi'.
I would have expected the result `abcdefghij'.

I expected that the array created by split() should have the
STRNUM attribute and be sorted (for asorti purpose) with numeric
comparison. In fact, asorti() puts the indice 10 between 1 and 2: this
is string comparison.

  s = "abcdefghij";
  split(s, a, "");
  n = asorti(a, b);
  for ( i = 1 ; i <= n ; i++ ) {
    printf("%s", a[b[i]]);

See also, from the documentation:

7.11 Sorting Array Values and Indices with `gawk'
... The comparison of array elements is done using `gawk''s usual
comparison rules (*note Typing and Comparison::)...

8.1.3 String-Manipulation Functions
... As array indices are always strings, the comparison performed is
always a string comparison. ...

5.10 Variable Typing and Comparison Expressions
... and the elements of an array created by `split' that are
numeric strings have the STRNUM attribute. ...

When two operands are compared, either string comparison or numeric
comparison may be used. This depends upon the attributes of the
operands, according to the following symmetric matrix:

             |       STRING          NUMERIC         STRNUM
     STRING  |       string          string          string
     NUMERIC |       string          numeric         numeric
     STRNUM  |       string          numeric         numeric

This e-mail is intended only for the above addressee. It may contain
privileged information. If you are not the addressee you must not copy,
distribute, disclose or use any of the information in it. If you have
received it in error please delete it and immediately notify the sender.
Security Notice: all e-mail, sent to or from this address, may be
accessed by someone other than the recipient, for system management and
security reasons. This access is controlled under Regulation of
Investigatory Powers Act 2000, Lawful Business Practises.

reply via email to

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