i'm trying abuse asort() (just because) copy array src array dest, no problem there:
$ awk 'begin { split("first;second;third",src,";") # make src array testing asort(src, dest, "@ind_num_asc") # copy array dest for(i in dest) print i, src[i], dest[i] # output }' 1 first first 2 second second 3 third third but there way use multidimensional array dest array? like:
asort(src, dest[src[1]], "@ind_num_asc") # or dest[src[1]][]
(former produces second argument not array, latter syntax error in reality first argument of split $0 , i'm trying group records.)
of course use for loop brain stuck on testing solution.
you need create array under dest[src[1]] first gawk knows dest[src[1]] array of arrays rather default array of strings:
$ cat tst.awk begin { split("first;second;third",src,/;/) # make src array testing asort(src, dest1d) # copy array dest1d for(i in dest1d) print i, src[i], dest1d[i] # output print "" dest2d[src[1]][1] asort(src, dest2d[src[1]]) # copy array dest2d for(i in dest2d) (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 index give initial sub-array it'll deleted asort(). see last example under https://www.gnu.org/software/gawk/manual/gawk.html#arrays-of-arrays:
recall reference uninitialized array element yields value of "", null string. has 1 important implication when intend use subarray argument function, illustrated following example:
$ gawk 'begin { split("a b c d", b[1]); print b[1][1] }' error→ gawk: cmd. line:1: fatal: split: second argument not arraythe way work around first force b[1] array creating arbitrary index:
$ gawk 'begin { b[1][1] = ""; split("a b c d", b[1]); print b[1][1] }' -|
Comments
Post a Comment