SPUG: Out of sorts with sort

Anthony Christopher anthonyc at blarg.net
Wed Dec 29 20:33:47 CST 1999


I have a hash that looks a lot like the hash in my example below.
My goal is to sort the keys of the hash based on the values of
'i 1' so I can do some processing in that particular order. The
commented lines indicate my best guesses as to how to do what I
want, but they don't work :-{
Is there a simple way to do this with sort or do I have to write
my own sort routine? 

#!/usr/local/bin/perl -w
my %hash = (
                 'keya' => { 'i 1' => 4 , 'i 2' => 1 },
                 'keyb' => { 'i 1' => 2 , 'i 2' => 2 },
                 'keyc' => { 'i 1' => 3 , 'i 2' => 1 },
                 'keyd' => { 'i 1' => 1 , 'i 2' => 2 }
                );
my @mykeys = keys %hash;
print join(',', at mykeys),"\n";
#my @sortedkeys = sort { $a->{'i 1'} <=> $b->{'i 1'} } @mykeys;
#my @sortedkeys = sort { $a{'i 1'} <=> $b{'i 1'} } @mykeys;
print join(',', at sortedkeys),"\n";

TIA

Anthony

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list