Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

sort hash after value from hash in hash

by ocs (Monk)
on Nov 24, 2006 at 14:37 UTC ( [id://585884]=perlquestion: print w/replies, xml ) Need Help??

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

Hi. I have a hash going like this:
$hash = { 1 => { key1 => string1, key2 => string2 }, 2 => { key1 => string3, key2 => string4 } }
I want to sort this thing after string1. I tried this one:
foreach my $key (sort { $hash->{'key1'} {$a} cmp $hash->{'key1'} {$b} +} keys %$hash) { ... }
I doesn't work :(
Is there a possibility to force the sort function to do so? :)
Thanx for any help!

Replies are listed 'Best First'.
Re: sort hash after value from hash in hash
by davorg (Chancellor) on Nov 24, 2006 at 14:46 UTC
    I want to sort this thing after string1

    It's not very clear exactly what you want to do. This will sort the hash by the values associated with 'key1' in each sub-hash.

    foreach my $key (sort { $hash->{$a}{key1} cmp $hash->{$b}{key1} } keys + %$hash) { ... }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Fine, thanks, thats doing exactly what I want :)
Re: sort hash after value from hash in hash
by rhesa (Vicar) on Nov 24, 2006 at 14:50 UTC
    Is there a possibility to force the sort function to do so? :)
    Sure there is! Once you realise what keys is iterating over, it becomes obvious.

    The keys of your $hash are 1 and 2, so that's what $a and $b refer to in the sort block.

    The final piece of the puzzle is going from there to the right value. It looks to me you want the value of "key1". That gives us:

    sort { $hash->{$a}{key1} cmp $hash->{$b}{key1} } keys %$hash
      Thanks to your explanation I finally got it. Now I can sort everything ;)
Re: sort hash after value from hash in hash
by shmem (Chancellor) on Nov 24, 2006 at 14:50 UTC
    $hash->{'string1'}{$a}

    That will not work. You can't lookup a hash key by it's value. You mean

    $hash->{$a}{'key1'}

    --shmem

    update: missed {$a}, corrected

    _($_=" "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}
      Yes, thats my mistake, sorry. In the original code it is right (I abstracted this one). I'm going to update this in my example. But nevertheless this solution doesn't work. But the both above mentioned do so :)
        Um, yes. I forgot about the $a,$b gotcha. <nit> Apart from that mistake, your example hash
        $hash = { 1 => { key1 => string1, key2 => string2 } 2 => { key1 => string1, key2 => string2 } }

        is very badly chosen, since the values for each key of the sub-hashes are identical. What do you expect sort to do for you? ,-) </nit>

        --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}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found