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

hi.... can any one explain me what's happening in the following cold...
use Tie::Hash::Sorted; my %ages = ( 'John' => 33, 'Jacob' => 29, 'Jingle' => 15, 'Heimer' => 48, 'Smitz' => 12, ); my $sort_by_numeric_value = sub { my $hash = shift; [ sort {$hash->{$b} <=> $hash->{$a}} keys %$hash ]; }; tie my %sorted_ages, 'Tie::Hash::Sorted', 'Hash' => \ %ages, 'Sort_Routine' => $sort_by_numeric_value; for my $name ( keys %sorted_ages ) { print "$name is $sorted_ages{$name} years old.\n"; }
thanks in advance

May 19, 2008 at 23:08 UTC, Janitored by McDarren. Added code tags

Replies are listed 'Best First'.
Re: meaning for the hash code
by Lu. (Hermit) on May 13, 2008 at 07:58 UTC
    You need to wrap your code between <code> tags, like that :
    use Tie::Hash::Sorted; my %ages = ( 'John' => 33, 'Jacob' => 29, 'Jingle' => 15, 'Heimer' => 48, 'Smitz' => 12, ); my $sort_by_numeric_value = sub { my $hash = shift; sort {$hash->{$b} <=> $hash->{$a}} keys %$hash ; }; tie my %sorted_ages, 'Tie::Hash::Sorted', 'Hash' => \ %ages, 'Sort_Routine' => $sort_by_numeric_value; for my $name ( keys %sorted_ages ) { print "$name is $sorted_ages{$name} years old.\n"; }
    The main part of this piece of code lies in the sub definition, which is, essentially, giving an explicit name to this particular use of sort, sorting the hash it's given by the numeric value of the values inside it.

    Using then the magic of Tie::Hash::Sorted, you apply this subroutine to %ages, and iterate through it to print out the names of the inviduals in ascending order of their ages.

    Hope that's clear enough.

    Lu.
Re: meaning for the hash code
by sandboxed (Sexton) on May 13, 2008 at 08:09 UTC

    Hi Tony1, first of all probably not so many monks would like to reply at the first stance: try to reformat the question and code using the rules available at How do I post a question effectively?. It's a nice practice and you'll see quickly a response (if available! :-) ).

    I think you're trying to sort some hash values with the module Tie::Hash::Sorted by numerical order. Is there happening any problem with the code or is it only a question about what it does? What's exactly what you don't understand?

    Update:I liked Lu's explanation, what else I could add? Thanks, Lu!

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: meaning for the hash code
by alexm (Chaplain) on May 13, 2008 at 13:00 UTC

    Completing sandboxed's comments, this code:

    my $sort_by_numeric_value = sub { my $hash = shift; [ sort {$hash->{$b} <=> $hash->{$a}} keys %$hash ]; }; tie my %sorted_ages, 'Tie::Hash::Sorted', 'Hash' => \ %ages, 'Sort_Routine' => $sort_by_numeric_value; for my $name ( keys %sorted_ages ) { print "$name is $sorted_ages{$name} years old.\n"; }

    Could've been replaced by:

    sub by_numeric_value { $ages{$b} <=> $ages{$a} } for my $name (sort by_numeric_value keys %ages) { print "$name is $ages{$name} years old.\n"; }
    Which is much more concise and easier to understand, IMO.
      hi, Thank you... now i can able to understand what's happened in the hash code.