A variation on the same theme, for sorting by value instead of key:
 
use strict;
use warnings;

my %hash = qw(1 A 2 B 3 C);

foreach my $key ( sort {$hash{$a} <=> $hash{$b} } keys %hash ) {
    print "$key => $hash{$key}\n";
}



        -----Original Message----- 
        From: Charles K. Clarkson [mailto:[EMAIL PROTECTED] 
        Sent: Mon 12/26/2005 6:40 AM 
        To: beginners@perl.org 
        Cc: 
        Subject: RE: sorting hash
        
        

        Anders Stegmann <mailto:[EMAIL PROTECTED]> wrote:
        
        : How do I do that?
        
            Use 'sort'.
        
        use strict;
        use warnings;
        
        my %hash = qw(1 A 2 B 3 C);
        
        foreach my $key ( sort { $a <=> $b } keys %hash ) {
            print "$key => $hash{$key}\n";
        }
        
        __END__
        
        
            The big question is: Are you using the right data
        structure for your solution? If you are sorting a hash
        more than once, you may want to use a sorted hash
        module or you may want to use a different data
        structure.
        
        
        
        HTH,
        
        Charles K. Clarkson
        --
        Mobile Homes Specialist
        254 968-8328
        
        
        
        
        --
        To unsubscribe, e-mail: [EMAIL PROTECTED]
        For additional commands, e-mail: [EMAIL PROTECTED]
        <http://learn.perl.org/> <http://learn.perl.org/first-response>
        
        
        

Reply via email to