Re: Sorting with GNU Awk

Liste des GroupesRevenir à cl awk 
Sujet : Re: Sorting with GNU Awk
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.awk
Date : 11. Feb 2025, 05:09:32
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <voeiht$1kej1$1@dont-email.me>
References : 1 2
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 11.02.2025 03:47, Kaz Kylheku wrote:
On 2025-02-11, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
[...]
Another question; is there a simple way for a _unique sorting_
like Unix'es 'sort -u'? - By simple I mean some setting, not a
programmed function like using associative array indexes, etc.
 
I don't think so, but if we consider that the indices of
an associative array must be unique, then it may be workable
to store the data as indices to "uniquefy" it, and use asorti
to sort indices rather than values.

I've had a separate 'uniq' function implemented based on
associative arrays; some temporary arrays were involved
and overall it looked quite clumsy. I've now rewritten it
using asorti() as you suggested, omitting the asort() and
my 'uniq' function. The test code first looked like this

    PROCINFO["sorted_in"] = "@val_num_desc"
    data = "6 9 11 9 22 3 6 2"
    n = split (data, arr)
    for (i in arr)
        pos[arr[i]]
    asorti (pos)
    for (i in pos)
        printf " %d", pos[i]
    printf "\n"

(My original data resides in a string, so it's a two-step
process; split into an indexed array, and then populate
the associative array.) That already works and fortunately
nothing is "lost" (as you formulate below). - So thanks
for the asorti hint.

But upon reconsideration I could also just omit the sorting
since I don't need the array object, I just need to iterate
in the correct order. I now have

    data = "6 9 11 9 22 3 6 2"
    n = split (data, arr)
    for (i in arr)
        pos[arr[i]]
    PROCINFO["sorted_in"] = "@ind_num_desc"
    for (i in pos)
        printf " %d", i
    printf "\n"

That works fine as well; and your asorti hint led me to the
second, even simpler variant to just use "@ind_num_desc",
i.e. defining iteration order on the _indices_.

Thanks again.

 
asorti inverts the array, sort of. The values of the array
are lost. The sorted indices become the values of the target
array, whose indices are the natural numbers (integers from 1).

Janis


Date Sujet#  Auteur
11 Feb 25 * Sorting with GNU Awk3Janis Papanagnou
11 Feb 25 `* Re: Sorting with GNU Awk2Kaz Kylheku
11 Feb 25  `- Re: Sorting with GNU Awk1Janis Papanagnou

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal