Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2015-11-25 01:19 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (667 votes), past polls