Jeff wrote: > Thanks for the response - some questions on your recommendation below: > > -----Original Message----- > From: david [mailto:[EMAIL PROTECTED]] > Sent: 17 September 2002 19:06 > To: [EMAIL PROTECTED] > Subject: Re: sorting a hash - multiple key fields > > >> the return statment is uneccessary. try something like: > >> 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"; >> } > > I had wondered about an || - lazy eval et al - would it be [better | no > different] using lower precedence 'or'? Using 'return' seems to > generalise better when I have a whole list of sort fields, as it can be > used in a loop construct?
in this case, '||' or 'or' will both work. you could use a return but there is no need for it. it won't make your code run faster. the comparison 'return' implicitly when it's done. > > You are the second person to suggest passing keys into the sort sub, and > getting it to lookup the child-hash - it seems to me it would be way > more efficient to pass in the hash values and not bother with the > lookup? > the reason why we suggest you using the keys instead of just using the values is simply because it's more flexiable. given the keys of the hash, you can always find out the corresponding value. given the values, however, do not neccessary means you can have the keys back. if you don't need the keys, simply use the values is sufficient. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]