当在子例程中隐式使用时,-1,0,1 如何帮助排序函数?
我在使用子例程时遇到了一些麻烦,即使用 sort 函数对数字数组进行排序。我知道如果您单独使用排序函数,它会使用 ASCII 格式进行排序,正如书中所描述的那样,它的排序不是所需的顺序。我知道并理解使用 <=>
比较值返回的内容,甚至被引入 cmp
来处理字符串(尽管我没有使用过)还没有)。
我具体不明白的是它是如何对它们进行数字排序的——实际的过程。我理解返回的内容,但书上只是说它返回 -1、0 和 1,而没有具体说明这些数字最终如何排序为 1 8 24 72 144 288
。
我的例子:
sub sort_by_number {
return $a <=> $b;
}
@myArray = (1,24,8,144,72,288);
foreach(sort sort_by_number(@myArray)) {
print("$_ ");
}
我完全理解的例子。这是完全有道理的,但我认为这主要是由于编码:
#!/usr/bin/perl
print("Please enter your name: ");
$name = <STDIN>;
chomp($name);
print("Please enter your age: ");
$age = <STDIN>;
chomp($age);
print(greeting($name, $age));
sub greeting {
$msg = "Hello $_[0], ";
determine_age($_[1],$msg);
}
sub determine_age {
$num = ($_[0] <=> 18);
if ($num == -1) {
return "$_[1]you are under 18.($_[0])\n";
} elsif ($num == 0) {
return "$_[1]you will be a 19 on your next birthday!\n";
} else {
return "$_[1]you are over 18!($_[0])\n";
}
}
如果有人能够澄清,我将非常感激。
I'm having some trouble with subroutines, namely the sort function being used to sort an array of numbers. I know if you use the sort function solely, it sorts using ASCII format, as the book describes this and that sorts in not the desired order. I'm aware and have an understanding of what gets returned by using <=>
to compare values, even being introdued to cmp
for strings (although I have not used it yet).
What I don't understand specifically is how it sorts them numerically - that actual process. I understand what is returned, but the book just says it returns -1, 0 and 1 and not how specifically the numbers end up finally sorted to 1 8 24 72 144 288
.
My example:
sub sort_by_number {
return $a <=> $b;
}
@myArray = (1,24,8,144,72,288);
foreach(sort sort_by_number(@myArray)) {
print("$_ ");
}
The example I understand fully. It makes perfect sense but I think that's mainly due to the coding:
#!/usr/bin/perl
print("Please enter your name: ");
$name = <STDIN>;
chomp($name);
print("Please enter your age: ");
$age = <STDIN>;
chomp($age);
print(greeting($name, $age));
sub greeting {
$msg = "Hello $_[0], ";
determine_age($_[1],$msg);
}
sub determine_age {
$num = ($_[0] <=> 18);
if ($num == -1) {
return "$_[1]you are under 18.($_[0])\n";
} elsif ($num == 0) {
return "$_[1]you will be a 19 on your next birthday!\n";
} else {
return "$_[1]you are over 18!($_[0])\n";
}
}
Massively appreciative if someone could clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的第二个示例不进行任何排序。
要回答“如何对它们进行数字排序 - 实际过程?”,Perl内部使用合并排序算法实现排序(Perl 5.6之前是快速排序)。
该算法本身相当复杂(有关详细信息,请参阅 Wiki),但它最终会比较 2 个数字并确定一个是否比另一个大,并根据该决定执行一些操作。如果您对细节感到好奇,需要比较的算法部分是 Wiki 示例中的
if first(left) ≤ first(right)
行。这就是自定义排序子例程的用武之地 - 它回答排序算法的“哪个数字更大”问题(或者更具体地说,一个数字是否小于或等于另一个数字)。
这种实现方式是,Perl 的
sort
将在内部调用“比较器”函数,并向其传递 2 个参数(通过别名$a
和$b
);如果第一个小于、等于或大于第二个,则期望函数返回负数、零或正数。Your second example doesn't do any sorting.
To answer "how it sorts them numerically - that actual process?", Perl internally implements sort using Merge Sort algorithm (was quicksort before Perl 5.6).
The algorithm itself is fairly complicated (See Wiki for details), BUT underneath it ends up comparing 2 numbers and deciding if one is bigger than the other, and performing some action depending on that decision. If you're curious about details, the algorithm part that needs the comparison is the
if first(left) ≤ first(right)
line on the Wiki example.This is where the custom sort subroutine comes in - it answer "which number is bigger" question for the sorting algorithm (or, to be more specific, whether one number is less than or equal than another).
The way this is implemented, Perl's
sort
will internally call the "comparator" function, and pass it 2 arguments (by aliases$a
and$b
); and expect the function to return negative, zero or positive if the first is less than, equal or greater than the second one.<=>
是一个数字运算符。当您比较$a <=>; $b
,它将返回 -1、0 或 1,具体取决于左侧参数在数值上是否小于、等于或大于右侧参数。sort
函数按对比较列表的元素,因此它需要一个带有两个参数(二元运算符)的比较函数。Sort
不断比较对并重新排序,直到每个项目都比列表中的下一个项目“更大”。<=>
is a numerical operator. When you compare$a <=> $b
, it will return -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument.The
sort
function compares elements of a list by pairs, so it needs a comparison function who takes two arguments (a binary operator).Sort
keeps comparing pairs and reordeing them until every item is "bigger" than following item in the list.