QM has asked for the wisdom of the Perl Monks concerning the following question:
I want a custom sort sub that can sort any hash, according to whatever sort mechanism I dream up, with the hash determined at runtime.
The examples I've been able to find all assume a specific global hash. Sort subs don't allow any parameter passing, so I'm wondering how to accomplish this with grace and efficiency. Short of an anonymous sub created for a specific hash, I'm not sure how to proceed.
As a counterexample, this doesn't cut it:
sub by_score { $score{$b} <=> $score{$a} }
Here, %score is hardcoded. I may want to use the same sub on more than 1 hash throughout the script, and would prefer not to write one for each hash.
-QM
--
Quantum Mechanics: The dreams stuff is made of
Back to
Seekers of Perl Wisdom