in reply to Custom, Reusable Sort Subroutine for Hashes?
Personally, I would create an anonymous subroutine that knows the correct hash to use. I think the fancy term would be "currying":
my @list = (qw( a b c d e f )); sub by_score { my( $score ) = @_; return sub($$) { my( $a,$b ) = @_; warn "$a / $b"; $score->{$a} <=> $score->{$b} } } my $by = by_score({ a => 5, b => 4, c => 3, d => 6, e => -1 }); print join ",", sort( {$by->($a,$b)} @list ); $by = by_score({ a => -5, b => 4, c => -3, d => 6, e => -1 }); print join ",", sort( {$by->($a,$b)} @list );
I'm a bit unhappy with the (lack of) syntax that prevents me from inlining $by. I would have liked to write something like:
$by = by_score({ a => -5, b => 4, c => -3, d => 6, e => -1 }); print join ",", sort( $by, @list );
... but that's nothing that Perl likes, as Perl doesn't know whether $by should be part of @list or not.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Custom, Reusable Sort Subroutine for Hashes?
by QM (Parson) on Aug 30, 2017 at 13:08 UTC |