Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting occurances and sort in descending oder 1

Status
Not open for further replies.

dvanan

Technical User
Sep 26, 2002
3
0
0
US
hi,

Need expert help.

I am using nawk to process a text file and posed with the foll problem :

Have an array that typically looks like this ;
a[0]= 32
a[1]= 10
a[2]= 32
a[3]= 14
a[4]= 16
a[5]= 10
a[6] = 32

I need to count the number of times each value occurs and then
sort them in desending order.
Actually I am trying to find the highest top three values.

so the result would typically look like
Top 1 - 32 - 3
Top 2 - 10 - 2
Top 3 - 14 - 1

Please advise.

Thanks,
dvanan
 
Hi dvanan,

Try this:

#!/usr/bin/awk -f

BEGIN{
a[0]=32
a[1]=10
a[2]=32
a[3]=14
a[4]=16
a[5]=10
a[6]=32

for ( i in a )
{
b[a]+=1;
}

for ( count=1; count<=3; count++)
{
highest=0;
for ( j in b )
{
if (highest < j){ highest=j }
}
print &quot;Top &quot; count &quot; - &quot; highest &quot; - &quot; b[highest];
delete b[j];
}

exit;
}

My results:
Top 1 - 32 - 3
Top 2 - 16 - 1
Top 3 - 14 - 1


Hope this is what you wanted.
Grant.
 
Hi Grant,

Thank you very much for your quick response and help.

The script does run but there seems to be some problem with the sorting ie the result should be

Top 1 - 32 - 3
Top 2 - 10 - 2
Top 3 - 14 - 1

as 10 occurs 2 times and 14 and 16 occur only once each.

Top 1 - 32 - 3
Top 2 - 16 - 1
Top 3 - 14 - 1


Also the when the elements of the array are change the output sorting does not reflect it.

Tried some debug but I'm quite new to the &quot;for ( a in b)&quot; syntax so was st(r)uck !

Pls advise.

Thanks,
dvanan
 
Hi dvanan,

Whoops! I misunderstood something. Let me take another look.

Grant.
 
Hi dvanan,

Here's the new version:

#!/usr/bin/awk -f

BEGIN{
a[0]=32
a[1]=10
a[2]=32
a[3]=14
a[4]=16
a[5]=10
a[6]=32

for ( i in a ) { b[a]+=1; }

for ( count=1; count<=3; count++)
{
highest=0;
for ( j in b )
{
if (highest < b[j]){ highest=b[j]; highindex=j; }
}
print &quot;Top &quot; count &quot; - &quot; highindex &quot; - &quot; b[highindex];
delete b[highindex];
}

exit;
}

Results:
Top 1 - 32 - 3
Top 2 - 10 - 2
Top 3 - 14 - 1

Hope that's better.
Grant.
 
Hi dvanan,

I forgot to turn off TGML and it treated a '' as a command to italicize.

So here is the new version once again:
#!/usr/bin/awk -f

BEGIN{
a[0]=32
a[1]=10
a[2]=32
a[3]=14
a[4]=16
a[5]=10
a[6]=32

for ( i in a ) { b[a]+=1; }

for ( count=1; count<=3; count++)
{
highest=0;
for ( j in b )
{
if (highest < b[j]){ highest=b[j]; highindex=j; }
}
print &quot;Top &quot; count &quot; - &quot; highindex &quot; - &quot; b[highindex];
delete b[highindex];
}

exit;
}

And the output:
Top 1 - 32 - 3
Top 2 - 10 - 2
Top 3 - 14 - 1

I'm full of mistakes today:)
Grant.
 
Grant,

works perfect &
That was just what I needed.

Thanks a lot !

have a nice day.
-dvanan
 
Hi dvanan,

In your previous message, you said:
>Tried some debug but I'm quite new to the &quot;for ( a in b)&quot; syntax so was st(r)uck !


It's quite straightforward. You can print each subscript in the array using the following syntax:

for ( variable in array )
{
print variable;
}


What you need to know is that the subscripts of arrays are just strings. The way they are stored internally is not in numeric order, even when the subscripts are all numeric. Instead they are stored in an order determined by some internal algorhythm.

So when you use the above syntax, the list of variables/elements that is generated by the above for loop will appear in a somewhat random order.




You probably have already figured it out, but you can print every value of the array using the following variation:

for ( variable in array )
{
print array[variable];
}



Another useful thing you can do is to test for the existance of a particular array element as follows:

if ( variable in array )
{
# do something...
}


And you can test for the non-existance as follows:
if ( ! (variable in array ) )
{
# do something...
}

Take care,
Grant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top