Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Sort of anonymous hash

by ccn (Vicar)
on Sep 29, 2009 at 16:51 UTC ( #798162=note: print w/replies, xml ) Need Help??


in reply to Sort of anonymous hash

Schwartzian transform

for my $d ( map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, $hash{$_}{desc} ] } keys %hash ) { }

Replies are listed 'Best First'.
Re^2: Sort of anonymous hash
by ikegami (Pope) on Sep 29, 2009 at 16:54 UTC
    Simpler and faster:
    for my $d ( sort { $hash{$a}{desc} cmp $hash{$b}{desc} } keys %hash ) { }
Re^2: Sort of anonymous hash
by johngg (Abbot) on Sep 29, 2009 at 19:30 UTC

    To expand on ikegami's response, using a Schwartzian Transform only makes sense when the item(s) by which to sort is/are obtained by some expensive transformation. That transformation gets repeated many times for each element being sorted as the sort algorith moves it into the right place by successive comparisons with other elements. There was no transformation required in your code as $hash{$_}{desc} could be accessed directly.

    Consider the following two code snippets that sort files by modification time (requiring an lstat call, our expensive transformation). Note the timings; using Benchmark would be better but this was just a quick example.

    knoppix@Knoppix:~$ time perl -Mstrict -wle ' -> my $d = q{/usr/bin}; -> my @fns = do { opendir my $dh, $d or die $!; readdir $dh }; -> my @sorted = -> sort { ( lstat qq{$d/$a} )[ 9 ] <=> ( lstat qq{$d/$b} )[ 9 ] } -> @fns;' real 0m1.614s user 0m0.307s sys 0m1.273s knoppix@Knoppix:~$ time perl -Mstrict -wle ' -> my $d = q{/usr/bin}; -> my @fns = do { opendir my $dh, $d or die $!; readdir $dh }; -> my @sorted = -> map { $_->[ 0 ] } -> sort { $a->[ 1 ] <=> $b->[ 1 ] } -> map { [ $_, lstat qq{$d/$_} ] } -> @fns;' real 0m0.281s user 0m0.100s sys 0m0.160s knoppix@Knoppix:~$

    I hope this is of interest.

    Cheers,

    JohnGG

      It is, indeed.
      ikegami's response was surely the most correct, because with no transformation involved, direct access is always faster.

      My question was to illustrate a point, i.e. if there was some perl placeholder to accomodate for an anonymous hash being created by another function.
        Yes. They're called variables.
        for my $d (sort {$descs->{$a} cmp $descs->{$b}} keys %{ $descs = {map +{$_ => $hash{$_}{desc}} keys %hash}} ) { ... ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://798162]
help
Chatterbox?
[Your Mother]: I say I do. :P
[Your Mother]: Missiles would be an idiot’s play. Putin is many things but he is not an idiot.
[erix]: I like the idea of the trumps troup of imbeciles growing slightly nervous now their incompetence will be tested
[erix]: you seem forget there is another idiot coming :)
[Your Mother]: The US government does not attract talent, but grafters and liars and it has been that way for the better part of a century. FEW exceptions.
[erix]: you think it only /looks/ worse this time?
[Your Mother]: HR Clinton was literally threatening Russia. She would have been MUCH more likely to start real trouble.
[Your Mother]: I think there is less decorum this time.
[Your Mother]: We’ve had rapists and thieves and unqualified government. They just had decorum.
[erix]: heh, understatement of the week :)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-01-20 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (173 votes). Check out past polls.