Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Scoping issue when sorting with subroutines

by extremely (Priest)
on Apr 12, 2002 at 19:17 UTC ( #158648=note: print w/ replies, xml ) Need Help??


in reply to Scoping issue when sorting with subroutines

Maybe something like this is what you wanted? It is rather on the dark side of the force but it may be what you want.

#!/usr/bin/perl -w use strict; my @l = (2, 4, 19, 3, 15, 30, 1, 31, 14); print sort {&{bysort("up")}} @l; print $/; print sort {&{bysort("dn")}} @l; print $/; sub bysort { my $by = shift; if ($by eq "up") { return sub { $a <=> $b }; } elsif ($by eq "dn") { return sub { $b <=> $a }; } else { return sub { $a cmp $b }; } }

--
$you = new YOU;
honk() if $you->love(perl)


Comment on Re: Scoping issue when sorting with subroutines
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-07-23 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (138 votes), past polls