Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asort(src,dest) to a multidimensional array

Tags:

I'm trying to abuse asort() (just because) to copy an array src to array dest, no problem there:

$ awk 'BEGIN {
    split("first;second;third",src,";") # make src array for testing
    asort(src, dest, "@ind_num_asc")    # copy array to dest
    for(i in dest) 
        print i, src[i], dest[i]        # output
}'
1 first first
2 second second
3 third third

But is there a way use a multidimensional array as the dest array? Something like:

asort(src, dest[src[1]], "@ind_num_asc") # or dest[src[1]][]

(former produces second argument not an array, latter syntax error In reality the first argument of split is $0 and I'm trying to group records.)

Of course I could use for loop but my brain is stuck on testing this solution.

like image 345
James Brown Avatar asked Sep 02 '16 09:09

James Brown


1 Answers

You just need to create an array under dest[src[1]] first so gawk knows that dest[src[1]] is an array of arrays rather than the default array of strings:

$ cat tst.awk
BEGIN {
    split("first;second;third",src,/;/) # make src array for testing

    asort(src, dest1d)              # copy array to dest1d
    for(i in dest1d)
        print i, src[i], dest1d[i]      # output
    print ""

    dest2d[src[1]][1]
    asort(src, dest2d[src[1]])          # copy array to dest2d
    for(i in dest2d)
        for (j in dest2d[i])
            print i, j, dest2d[i][j]    # output
}

$ gawk -f tst.awk
1 first first
2 second second
3 third third

first 1 first
first 2 second
first 3 third

It doesn't matter what index you give that initial sub-array as it'll get deleted by the asort(). See the very last example under https://www.gnu.org/software/gawk/manual/gawk.html#Arrays-of-Arrays:

Recall that a reference to an uninitialized array element yields a value of "", the null string. This has one important implication when you intend to use a subarray as an argument to a function, as illustrated by the following example:

$ gawk 'BEGIN { split("a b c d", b[1]); print b[1][1] }'
error→ gawk: cmd. line:1: fatal: split: second argument is not an array

The way to work around this is to first force b[1] to be an array by creating an arbitrary index:

$ gawk 'BEGIN { b[1][1] = ""; split("a b c d", b[1]); print b[1][1] }'
-| a
like image 179
Ed Morton Avatar answered Sep 24 '22 14:09

Ed Morton