[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: asorti() comparison
From: |
Aharon Robbins |
Subject: |
Re: asorti() comparison |
Date: |
Wed, 20 Sep 2006 19:24:18 +0300 |
Greetings. Re this:
> Date: Tue, 19 Sep 2006 08:05:44 +0200
> From: "EXCOFFIER, Denis (CS SYSTEMES INFORMATION)" <address@hidden>
> Subject: asorti() comparison
> To: address@hidden
> Cc: "EXCOFFIER, Denis (CS SYSTEMES INFORMATION)" <address@hidden>
>
> Hello,
>
> 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.
The key point of information is this bit that you quoted:
> 8.1.3 String-Manipulation Functions
> ===================================
> ... As array indices are always strings, the comparison performed is
> always a string comparison. ...
Looking at your program:
> --foo.awk---------------------
> BEGIN {
> s = "abcdefghij";
> split(s, a, "");
> n = asorti(a, b);
> for ( i = 1 ; i <= n ; i++ ) {
> printf("%s", a[b[i]]);
> };
> printf("\n");
> }
> ------------------------------
Here is what happens.
1. The call to split() creates the array a[]. The indices of a[] are
strings. These strings represent numbers, but nonetheless they are pure
string values.
2. The call to asorti() creates b[], using the indices of a[] as the
values in b[] on which to do the sorting. Since the values are pure
strings, they are compared and sorted as strings, producing the behavior
you saw.
You stated:
> 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.
The STRNUM attribute would apply only to the _values_ (the elements)
in the array created by split(), not to the _indices_.
To make things work as you suggest would break the defined awk semantics.
It might or might not have an effect in other ways than just asorti().
Hmmm. I haven't tried it, but something along the lines of
n = asorti(a, b)
for (i in b)
b[i] += 0
asort(b)
might give you what you're looking for.
Hope this helps,
Arnold
- asorti() comparison, EXCOFFIER, Denis (CS SYSTEMES INFORMATION), 2006/09/19
- Re: asorti() comparison,
Aharon Robbins <=