"strange" behaviour with asort..

Anonymous
2010-10-19
2013-04-22

  • Anonymous
    2010-10-19

    Hi

    I get puzzling result from this code snippet:

    BEGIN {
    a["last"] = "de"
    a["first"] = "sac"
    a["middle"] = "cul"
    len=asort(a)
    for (i=1;i<=len;i++){
    print i " " a[i]
    }
    }
    

    Output is:
    $ parse test2
    Compiling test2.awk to binary test2
    1 sac
    2 de
    3 cul
    with awka and:
    $ gawk -f test2.awk
    1 cul
    2 de
    3 sac
    $
    with gawk..

    I wonder what's happening here…

    Cheers
    Orvar

     

  • Anonymous
    2010-10-19

    Ooops …

    the code should have been..

    BEGIN {
    a["last"] = "de"
    a["first"] = "sac"
    a["middle"] = "cul"
    len=asort(a)
    for (i=1;i<=len;i++){
    print i " " a[i]
    }
    
     

  • Anonymous
    2010-10-19

    Wtf… ahhh ok _ is an BBCode tag.. so it disappeared..

    Here is the code (again)..

    BEGIN {

    a = "de"
    a = "sac"
    a = "cul"

    len=asort(a)
    for (i=1;i<=len;i++){
    print i " " a
    }
    _

     

  • Anonymous
    2010-10-19

    Gahhh.. impossible

     

  • Anonymous
    2010-10-19

    try try try again…

    BEGIN {
    a["last"] = "de"
    a["first"] = "sac"
    a["middle"] = "cul"
    len=asort(a)
    for (j=1;j<=len;j++){
    print j " " a[j]
    }
    }
    
     

  • Anonymous
    2010-10-19

    Hi

    I think I believe I have found what is going on… 

    awka is not sorting the values but sorts the indices…  which is confusing and quite.. useless…

    But I'm not giving up on my new love awka just because "she" is inconsequential and confusing… for a long and lasting relationship both parties has to learn to live with each others faults.. as undoubtedly someone very wise once said..

    I WILL figure a workaround out…

    // A_big_fan

    BEGIN {
    a["b"]="second"
    a["c"]="third"
    a["a"]="first"
    a["1"]="number one"
    len=asort(a)
    for (j=1;j<=len;j++){
    print j " - " a[j]  
    }
    }
    

    gives the output:
    $ parse test2
    Compiling test2.awk to binary test2
    1 - number one
    2 - first
    3 - second
    4 - third
    $

     

  • Anonymous
    2010-10-22

    .. ha ha..

    Not soo useless after all..? Maybe this really was the gawk 3.1.0 behavior? Anyway it has changed and a workaround is needed..

    I have so far investigated 3 distinctly different approaches.. using external sort cmd, original awk quicksort implementation and my favorite using the "wierd" awka asort which turned out to be the fastest and the simplest to code..

    It is still not fast as in awka fast and for medium arrays <100000 gawk is faster by 50% wich sucks but overall awka preformance is still MUCH higer..

    Maybe an addon to awka anyone? and when you are at it pls code asorti for completness…

    I will report these "findings" in a seperate thread with code if anyone is interested..

    I'll call it: asort "bug" solved..

    /The_biggest_awka_fan!!?