Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: sorting function arcument to 'sort': Can I pass additional parameters?

by blazar (Canon)
on Nov 10, 2008 at 10:22 UTC ( #722605=note: print w/ replies, xml ) Need Help??


in reply to sorting function arcument to 'sort': Can I pass additional parameters?

I personally believe that you don't want to "pass in other arguments to the sorting sub" because the sub is meant to be used sort and sort() itself won't pass anything in. Chances are you want to use closures instead:

#!/usr/bin/perl use strict; use warnings; use 5.010; my @trans = ( sub { shift }, sub { ~ shift } ); my @unsorted = qw/foo bar baz/; for my $tr (@trans) { my @sorted = sort { $tr->($a) cmp $tr->($b) } @unsorted; say "@sorted"; } __END__
--
If you can't understand the incipit, then please check the IPB Campaign.


Comment on Re: sorting function arcument to 'sort': Can I pass additional parameters?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-12-28 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls