views:

73

answers:

2

I'm using sort with a customized comparison subroutine I've written:

sub special_compare {
 # calc something using $a and $b
 # return value
}

my @sorted = sort special_compare @list;

I know it's best use $a and $b which are automatically set, but sometimes I'd like my special_compare to get more arguments, i.e.:

sub special_compare {
 my ($a, $b, @more) = @_; # or maybe 'my @more = @_;' ?
 # calc something using $a, $b and @more
 # return value
}

How can I do that?

+5  A: 

Use the sort BLOCK LIST syntax, see perldoc -f sort.

If you have written the above special_compare sub, you can do, for instance:

my @sorted = sort { special_compare($a, $b, @more) } @list;
mscha
+1 nice and simple!
David B
You may want to pass a reference to `@more` to stop it being copied all the time.
ar
Or just use in-line code instead of calling a sub, if appropriate.
mscha
+1  A: 

You can use closure in place of the sort subroutine:

my @more;
my $sub = sub {        
    # calc something using $a, $b and @more
};

my @sorted = sort $sub @list;

If you want to pass the elements to be compared in @_, set subroutine's prototype to ($$). Note: this is slower than unprototyped subroutine.

eugene y
mscha's solution is cleaner and seems to work. Do you see any reason not using it?
David B
@David: using closure may be more efficient. I didn't run benchmark though.
eugene y
If performance is a consideration and the comparison function is heavy, have a look at the [Schwartzian transform](http://en.wikipedia.org/wiki/Schwartzian_transform).
mscha