"SHAKARIAN,SERGE (HP-NewJersey,ex1)" wrote:
> Hello,
>
> I think I understand the <=> operator, it returns -1 if left operand is less
> then right, 0 if equal and +1 if left is greater then right. What I dont
> understand is why this works:
>
> @list = sort { $a<=>$b } ( @list ); # lowest to highest if $a and $b are
> reversed highest to lowest
>
> is there a way for the compare operator to return the higher or lower
> operand?
>
> Thanks,
> Serge
Hi Serge,
I'm not sure of the mechanism for stting up infix operators like the <=>, but given
that this works, the rest sort of flows. Apparently $a aand $b are package globals
which alway represent theleft and right operands, respectively, of an infix operator.
>From perldoc perlvar:
$a
$b Special package variables when using sort(), see "sort" in
perlfunc. Because of this specialness $a and $b don't need to be
declared (using local(), use vars, or our()) even when using the
strict vars pragma. Don't lexicalize them with "my $a" or "my
$b" if you want to be able to use them in the sort() comparison
block or function.
It works. that is what counts. Whether you are using the operator or the cmp
string-comparison operator, the comparison parameter defines the total-order semantics
for the sort. The sort subroutine itself takes the output of the comparison and uses
a standard sort routine to apply the comparison through the array specified. The
implementation of sort has been evolving, and probably will continue to.
perldoc sort
Joseph
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]