Gossamer Forum
Home : General : Perl Programming :

Newbie help please...

Quote Reply
Newbie help please...
In my script I have the hash "%COUNTED" containing the following name:value pairs "n10:116 n11:110 n12:110 n13:106 n30:97 n14:103 n15:137 n31:109 n16:113 n32:104 n17:109" I want to sort this in descending order according to the value then be able to assign $name = %name $value = %value so that I can make the necessary calculations from that. Everything I have tried ends up seperating the name from its value and sorting them seperately. Can someone PLEASE tell me how to accomplish my this task. Thank You in advance!
Quote Reply
Re: [CraigB] Newbie help please... In reply to
What calculation do you wan't to do? ....you shouldn't have to extract anything from the hash.

To sort....

Code:
my @keys = sort { $COUNTED{$b} <=> $COUNTED{$a} } keys %COUNTED;
print "$_ => $COUNTED{$_}<br>" for (@keys);

Last edited by:

Paul: Apr 15, 2002, 11:30 AM
Quote Reply
Re: [Paul] Newbie help please... In reply to
THANK YOU!!! That sorts everything perfectly!! Laugh

As far as extracting data from the hash, I need to use the values of each pair to calculate percentages and averages upon another number generated earlier in the script. I then need to use both the name and value pairs within html tables for display.