Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

RE: Dynamic sort algorithm selection

by quidity (Pilgrim)
on Nov 15, 2000 at 05:42 UTC ( #41695=note: print w/ replies, xml ) Need Help??


in reply to Dynamic sort algorithm selection

This does actually work the way you expected, set up some subs with known names, then give the sort routine a variable containing that name:

sub ascii {$a cmp $b} sub alpha {lc($a) cmp lc($b)} @a = qw(a A b B D d c C); $subname = 'ascii'; @sorted = sort $subname @a; print @sorted; $subname = 'alpha'; @sorted = sort $subname @a; print @sorted;

If you are having trouble with an earlier version of perl, then first try upgrading, failing that, you can always use eval:

 eval '@sorted = sort ' . $subname . ' @unsorted';

But that's icky, so try not to do that.


Comment on RE: Dynamic sort algorithm selection
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://41695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2015-07-04 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls