SPUG: Out of sorts with sort

Scott Blachowicz sab at rresearch.com
Wed Dec 29 20:42:33 CST 1999


On Wed, Dec 29, 1999 at 06:33:47PM -0800, Anthony Christopher wrote:
> #!/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";

How about something more like:

  my @sortedkeys = sort {
	$hash{$a}->{'i 1'} <=> $hash{$b}->{'i 1'}
  } @mykeys;

In other words...you forgot that $a & $b are keys into %hash...

Scott

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    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