本文介绍了"$ a"到底是什么?和"$ b"在Perl的"sort()"中功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何通过使用Perl的sort()函数获得所需的结果,这更多地是关于sort()的内部工作原理的问题.

I understand how to get the results I want from using Perl's sort() function, this is more a question about the inner workings of sort().

"$ a"和"$ b"变量从何而来?我通读了文档以进行排序,似乎不清楚.什么是"$ a"和"$ b",它们为何与众不同?

Where do the "$a" and "$b" variables come from? I read through the documentation for sort and it seems unclear. What are "$a" and "$b" and what makes them special?

例如:

my @sorted_list = sort {$a cmp $b} @unsorted_list;

排序如何知道如何处理"$ a"和"$ b",为什么不出现"$ a"或"$ b"的全局符号要求显式包名称"错误?

How does sort know what to do with "$a" and "$b" and why don't you get the "Global symbol requires explicit package name" error for "$a" or "$b"?

推荐答案

$a$b是免税的全局变量;它们是免税的,因为Perl允许在任何地方使用它们而无需声明.它们由排序功能设置.在严格模式下使用任何其他未声明的全局排序将触发错误.

$a and $b are exempt global variables; they are exempt in that Perl allows them to be used (anywhere) without being declared. They are set by the sort function. Use of any other undeclared global in sort (in strict mode) will trigger an error.

sort函数接受各种形式的输入,一种是代码块,这就是您所引用的形式.

The sort function accepts various forms of input, one being a code block, which is the form you are referring to.

{$a cmp $b}是一个代码块,它被解析并作为代码块"传递给sort函数,Perl会检查sort的参数,如果接收到代码块,则sort将设置$a$b(如果它们作为代码块内的程序包全局变量存在),并将已排序的每一对项目分配给$a$b.您所要做的就是参考它们来控制排序算法.否则,将使用内部算法(我认为这是合并排序).

{$a cmp $b} is a code block, it is parsed and passed as a "chunk of code" to the sort function, and Perl checks the arguments for sort and if it receives a code block, sort will set $a and $b, if they exist as package globals within the code block, and assign each pair of items being sorted to $a and $b. All you have to do is refer to them to control the sort algorithm. Otherwise, the internal algorithm is used (which I think is merge sort).

http://perldoc.perl.org/functions/sort.html

$a$b不是词法,它们是程序包全局变量(或只是全局变量).

$a and $b are not lexicals, they are package globals (or just globals).

在主体中,您可以编写:

In a main you can write:

sort {$main::a cmp $main::b} @list;

或者在另一个程序包中,您可以编写:

Or in another package, you could write:

package foo;

sort {$foo::a cmp $foo::b} @list;

您实际上不应该像这样前缀.我正在演示$a$b实际上是当前程序包中的全局变量,而不是sort函数中的某些魔术$a,尽管Perl知道即使使用严格模式也可以定义它们.

You shouldn't actually prefix like this; I am demonstrating that $a and $b are actually globals within your current package, and not some magic $a within the sort function, although Perl knows to allow you to define them even with strict mode.

您不能只使用任何变量(在严格模式下).试试:

You can't just use any variables (in strict mode). Try:

sort {$A cmp $B} @list;

Global symbol "$A" requires explicit package name at sort.pl

您不能在排序范围内使用词汇(我的$ a).

You cannot use a lexical (my $a) in scope of sort.

my $a;
sort {$a cmp $b} @list;

Can't use "my $a" in sort comparison at sort.pl line 13.

$a$b在Perl中是 anywhere 的特殊地方.尽管sort是豁免的原因,但它们不受严格模式的约束,而严格模式与排序无关.

$a and $b are special anywhere in Perl. They are exempt from strict mode, which is unrelated to sort, though sort was the reason for the exemption.

这篇关于"$ a"到底是什么?和"$ b"在Perl的"sort()"中功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:06