Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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 having an uproarious good time at the Monastery: (6)
As of 2014-09-02 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (19 votes), past polls