我可以传递参数到Perl中sort的compare子例程吗? [英] Can I pass arguments to the compare subroutine of sort in Perl?
本文介绍了我可以传递参数到Perl中sort的compare子例程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 sort
与我写的自定义比较子程序:
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;
我知道最好使用 $ a
$ b
这是自动设置的,但有时我想让我的 special_compare
得到更多的参数, p>
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
}
我如何做呢?
推荐答案
/ code>语法,请参见 perldoc -f sort 。
Use the sort BLOCK LIST
syntax, see perldoc -f sort.
如果你写了上面的 special_compare
sub,你可以做,例如:
If you have written the above special_compare
sub, you can do, for instance:
my @sorted = sort { special_compare($a, $b, @more) } @list;
这篇关于我可以传递参数到Perl中sort的compare子例程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文