Jeff Aa wrote:

> Folks,
> 
> I want to sort my masked hashes into neat little piles for easier
> digestion:
> Please note this is _example_ data 8-)
> 
> my $h = {
>   a => { name => 'apple', taste => 3 },
>   b => { name => 'peach', taste => 2 },
>   c => { name => 'banana', taste => 2 },
> }
> 
> I want to sort first on taste and then on name, [and in the real world
> on other things as well..] ATM I do this
> 
> foreach my $item ( sort
>   {
>     return $a->{taste} <=> $b->{taste} unless $a->{taste}==$b->{taste};
>     return $a->{name} cmp $b->{name};
>   } values %$h ) {
>   print "Name: $item->{name} taste: $item->"taste}\n";
> }
> 

the return statment is uneccessary. try something like:

#!/usr/bin/perl -w
use strict;

my $h = { a => { name => 'apple', taste => 2 },
          b => { name => 'peach', taste => 1 },
          c => { name => 'banana', taste => 3},
          d => { name => 'peach', taste => 2}};

foreach my $k (sort {$h->{$a}->{taste} <=> $h->{$b}->{taste} ||
                     $h->{$a}->{name} cmp $h->{$b}->{name}} keys %{$h}){
        print "$h->{$k}->{name} $h->{$k}->{taste}\n";
}

__END__

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to