Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Sort Hash of Hash numerically

by duelafn (Vicar)
on Jun 13, 2011 at 14:33 UTC ( #909390=note: print w/replies, xml ) Need Help??

in reply to Re: Sort Hash of Hash numerically
in thread Sort Hash of Hash numerically

Is reverse sort optimized away (it would be cool if it swapped the meaning of $a and $b) or do you really mean:

for (sort { $HoH{$b}{head_count} <=> $HoH{$a}{head_count} } keys %HoH) + { print "$_ House Type: $HoH{$_}{house_type} Head Count: $HoH{$_}{he +ad_count}\n"; }

Update: toolic++. I went searching for the definition of "newer perls" and found that this optimization has been around for some time. From perl586delta:

reverse sort ... is now optimized to sort in reverse, avoiding the generation of a temporary intermediate list.

for (reverse @foo) now iterates in reverse, avoiding the generation of a temporary reversed list.

Good Day,

Replies are listed 'Best First'.
Re^3: Sort Hash of Hash numerically
by toolic (Bishop) on Jun 13, 2011 at 14:50 UTC
    I really meant to use reverse. According to Perl::Critic::Policy::BuiltinFunctions::ProhibitReverseSortBlock
    Conway says that it is much clearer to use reverse than to flip $a and $b around in a sort block. He also suggests that, in newer perls, reverse is specifically looked for and optimized, and in the case of a simple reversed string sort, using reverse with a sort with no block is faster even in old perls.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://909390]
Discipulus s/silbing/whistle/ dunno from where this word was in my mind..
[Discipulus]: lost in time in space and in the meaning (rocky horror picture show)
[Corion]: Discipulus: Yeah, a short vacation on the Canary islands (Lanzarote). Always great to be in the warmth and sun, from a cold and rainy February here ;)
[Corion]: choroba: Yeah, you have to find a time for the regular skype (or whatever) meetup, I imagine that's hard
[choroba]: Lanzarote is great!
choroba went there year ago

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-02-22 08:48 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (325 votes). Check out past polls.