> is there a way for the compare operator to
> return the higher or lower operand?
No, that is not what it does.
> @list = sort { $a<=>$b } ( @list );
> I dont understand is why this works:
I'm not sure how much you know, so forgive me if I give you info on what you
already understand.
A sort algorythm goes through the list in some way (depending on the
algoruthm used). As it goes through it compares 2 values at a time, and
does something based on the comparison. For every 2 values evaluated Perl
runs the sub you gave it (in this case {$a<=>$b}). ...It puts the first
value in $a, the second in $b, and leaves it up to you to decide which is
higher/lower. You tell Perl this by returning -1, 0, or 1.
This lets you basically sort any which way you want.
Numeric sort:
{ $a <=> $b }
String sort (case sensitive):
{ $a cmp $b }
String sort (case insensitive):
{ lc($a) cmp lc($b) }
Is that what you were looking for?
Rob
-----Original Message-----
From: SHAKARIAN,SERGE (HP-NewJersey,ex1) [mailto:[EMAIL PROTECTED]
Sent: Friday, March 21, 2003 6:29 PM
To: [EMAIL PROTECTED]
Subject: spaceship operator <=> question
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
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]