Caution: if you have any data that is repetitive, you will lose information
during the flipping process.

Subject: Re: Sorting a hash by value, and displaying the key



--- David Michael <[EMAIL PROTECTED]> wrote:
> Hello,
>     I have a hash that needs to be displayed in a certain order. I
> tried
>     
>     foreach $key (sort (keys %HASH)) {
>         print $key;
>     }
> 
>     that sorts alphabetically. I need it in the order it was
> inserted, so I made the value a number that increased for each key. I
> need to sort by the value, but display the key. Any ideas ?

Try flipping the hash.

 my %flipped;
 @flipped{values %HASH} = keys %HASH;

That makes the keys of %HASH the values of %flipped, with the matching
values of %HASH being the keys to which they are assigned.
Now just do it the easy way.

 for my $key (sort keys %flipped) {
   print $flipped{$key};
 }

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Reply via email to