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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Custom, Reusable Sort Subroutine for Hashes?
by choroba (Cardinal) on Aug 30, 2017 at 11:09 UTC | |
by QM (Parson) on Aug 30, 2017 at 12:45 UTC | |
by choroba (Cardinal) on Aug 30, 2017 at 12:56 UTC | |
by LanX (Saint) on Aug 30, 2017 at 15:41 UTC | |
Re: Custom, Reusable Sort Subroutine for Hashes?
by Corion (Patriarch) on Aug 30, 2017 at 11:15 UTC | |
by QM (Parson) on Aug 30, 2017 at 13:08 UTC | |
Re: Custom, Reusable Sort Subroutine for Hashes?
by QM (Parson) on Sep 05, 2017 at 16:58 UTC | |
by karlgoethebier (Abbot) on Sep 05, 2017 at 18:58 UTC | |
by QM (Parson) on Sep 06, 2017 at 12:37 UTC | |
by karlgoethebier (Abbot) on Sep 06, 2017 at 12:57 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom