Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: double sort HoH by value/key

by mwah (Hermit)
on Nov 21, 2007 at 22:13 UTC ( [id://652261]=note: print w/replies, xml ) Need Help??


in reply to double sort HoH by value/key

You have been close. Johngg did already post one solution (he was faster than me ;-)

Another variant would be:

use strict; use warnings; my %HoH = ( a => { value1 => "foo", value2 => 1 }, e => { value1 => "bar", value2 => 2 }, b => { value1 => "foo", value2 => 2 }, f => { value1 => "bar", value2 => 2 }, d => { value1 => "foo", value2 => 3 }, c => { value1 => "bar", value2 => 5 }, h => { value1 => "foo", value2 => 4 }, g => { value1 => "foo", value2 => 4 } ); my @sorted = sort HoHsort keys %HoH; sub HoHsort { $HoH{$b}{value2} <=> $HoH{$a}{value2} || $a cmp $b } print map "$_ =>=> { $HoH{$_}{value2} } \n", @sorted;

Regards

mwa

Replies are listed 'Best First'.
Re^2: double sort HoH by value/key
by rb1980 (Initiate) on Nov 21, 2007 at 22:31 UTC
    I think my trouble is in understanding what $a and $b are referring to. I find it hard to comprehend how sort can "see outside" of the foreach loop. I think I get it now, though. I am going to try to read up more on sort.
      I think my trouble is in understanding what $a and $b are referring to.

      They are referring to the elements you pass into the sort routine. Which element of that list gets associated when to $a or to $b is sort's business. doc;//sort assigns elements of that list to $a and $b as aliases, as the underlying sort algorithm demands.

      You were passing keys %HoH to the sort function, hence the keys of the outer hash, the chars (a .. g) in the order perl's hashing implementation sees fit. $a and $b, inside the sort function block, will never be anything else than an alias to an element of the list you passed in.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      I find it hard to comprehend how sort can "see outside" of the foreach loop.

      That's common for all blocks.

      my $var = "Hi!"; { print("$var\n"); }
      my $var = "Hi!"; for (1..2) { print("$var\n"); }
      my $var = "Hi!"; print(map { "$var\n" } 1..2);
      my $var = "Hi!"; sub func { print("$var\n"); } func();

      Yes, the following works even though $var goes out of scope before func is called.

      { my $var = "Hi!"; sub func { print("$var\n"); } } func();

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found