August 26, 2005

AWK sort array

GAWK provides asort and asorti functions to sort arrays, but it is not useful in sorting arraying while wanting to keep the relationship between the index and the value. Here are two functions, zsort and zsorti, that do this.

Here is the code

#!/bin/gawk -f
function zsort(a,b){
count=0;
delete b;
sort= "LC_ALL=C sort -k2nr -t,"
for (i in a){
printf("%s,%s\n",i,a[i]) |& sort;
}
close(sort, "to")
while ((sort |& getline line) > 0){
split(line,x,",");
count++;
b[count,1]=x[1];
b[count,2]=x[2];
}
close(sort)
return count;
}
function zsorti(a,b){
count =0;
delete b;
sort= "LC_ALL=C sort -k1nr -t,"
for (i in a){
printf("%s,%s\n",i,a[i]) |& sort;
}
close(sort, "to")
while ((sort |& getline line) > 0){
split(line,x,",");
count++;
b[count,1]=x[1];
b[count,2]=x[2];
}
close(sort)
return count;
}
BEGIN{
a["x"]=5; a["y"]=7; a["z"]=6; a["a"]=10;

# sort by the array value
num=zsort(a,b);
for (i=1;i<=num;i++){
printf("%s,%s\n",b[i,1],b[i,2]);
}
print "==============="
# sort by the array index
num=zsorti(a,b);
for (i=1;i<=num;i++){
printf("%s,%s\n",b[i,1],b[i,2]);
}
}


Here is the result:

a,10
y,7
z,6
x,5
===============
a,10
x,5
y,7
z,6

No comments:

Post a Comment