in reply to Re: double sort HoH by value/key
in thread double sort HoH by value/key

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.

Replies are listed 'Best First'.
Re^3: double sort HoH by value/key
by shmem (Chancellor) on Nov 21, 2007 at 23:40 UTC
    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}
Re^3: double sort HoH by value/key
by ikegami (Patriarch) on Nov 21, 2007 at 23:55 UTC

    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();