Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Sort of anonymous hash

by larsss31 (Acolyte)
on Sep 29, 2009 at 16:21 UTC ( [id://798152]=perlquestion: print w/replies, xml ) Need Help??

larsss31 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
i have a question regarding the sorting of on-the-fly created hashes.

Consider the following code:

for my $d (sort {$a cmp $b} keys %{{map {$_ => $hash{$_}{desc}} keys % +hash}} ) { ... ... }

that compares keys, by key value, of a map-created hash and outputs a list.
Fair straightforward, so far.
The question being: how can i sort those keys BY HASH VALUE, considered that i don't have an hash name to prepend to the key.

I mean, the classic sort { $hash{$a} cmp $hash{b} } keys %hash, but without an "%hashname".

Thanks for all your tips

larss

Replies are listed 'Best First'.
Re: Sort of anonymous hash
by ikegami (Patriarch) on Sep 29, 2009 at 16:47 UTC
    for my $d (sort {$a cmp $b} keys %{{map {$_ => $hash{$_}{desc}} keys % +hash}} ) { ... ... }
    is unreadable. It should be
    my %descs = map { $_ => $hash{$_}{desc} } keys %hash; for my $d ( sort keys %descs ) { ... ... }
    The answer to your answer is now obvious.
    my %descs = map { $_ => $hash{$_}{desc} } keys %hash; for my $d ( sort { $descs{$a} cmp $descs{$b} } keys %desc ) { ... ... }

    Update: On the other hand, why are you building a hash at all??

    my %descs = map { $_ => $hash{$_}{desc} } keys %hash; for my $d ( sort { $descs{$a} cmp $descs{$b} } keys %descs ) { my $desc = $descs{$d}; ... }
    can be written as
    for my $d ( sort { $hash{$a}{desc} cmp $hash{$b}{desc} } keys %hash ) +{ my $desc = $hash{desc}{$d}; ... }
      if i wanted to use

      my %descs = map { $_ => $hash{$_}{desc} } keys %hash; for my $d ( sort keys %descs ) { ... ... }

      i had written that.
      The question was about not using an explicit intermediate hash.
      Thanks anyway for your contribute
Re: Sort of anonymous hash
by ccn (Vicar) on Sep 29, 2009 at 16:51 UTC
      Simpler and faster:
      for my $d ( sort { $hash{$a}{desc} cmp $hash{$b}{desc} } keys %hash ) { }

      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.
Re: Sort of anonymous hash
by Anonymous Monk on Sep 29, 2009 at 16:44 UTC
    Perhaps it is too obvious, but you could change keys %{hash} to values %{hash}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://798152]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found