in reply to meaning for the hash code

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.

Replies are listed 'Best First'.
Re^2: meaning for the hash code
by Tony1 (Novice) on May 14, 2008 at 06:23 UTC
    hi, Thank you... now i can able to understand what's happened in the hash code.