Thanks to Charles, Jonathan and Members !

I found out that if the original list in the array is not well sorted, then
sort "spaceship" will compare the number of times based on the number of
list in that element, for eg
3 elements will permutate to 3 times,
4 elements will permutate to 6 times etc....
There's a formula to calculate permutation anyway!

I've also confirmed that if the value within the spaceship return 1 or 0, it
does not switch position.
It will only switch position if it return a value -1.

Strangely, contrary to what I would have thought, the spaceship operator
puts the second element into $a and the first element into $b.

Anyway, heres what exactly happening within the "spaceship operator" if
members wishes to know :-
Script below.

Legend :-
Stay means dont flip position.
Flip means to swop the position between that 2 elements.

Unsorted nos => 1 2 3 4
******************************
1 $a = 2 whilst $b = 1 , value = 1   Stay     results  1234
2 $a = 3 whilst $b = 2 , value = 1   Stay     same
3 $a = 4 whilst $b = 3 , value = 1   Stay     same

1,2,3,4

******************************
Unsorted nos => 4 3 2 1
******************************
1 $a = 3 whilst $b = 4 , value = -1   Flip      results  3421
2 $a = 2 whilst $b = 4 , value = -1   Flip      results  3241
3 $a = 2 whilst $b = 3 , value = -1   Flip      results  2341
4 $a = 1 whilst $b = 4 , value = -1   Flip      results  2314
5 $a = 1 whilst $b = 3 , value = -1   Flip      results  2134
6 $a = 1 whilst $b = 2 , value = -1   Flip      results  1234

1,2,3,4

******************************
Unsorted nos => 3 4 2 1
******************************
1 $a = 4 whilst $b = 3 , value = 1   Stay     results  3421
2 $a = 2 whilst $b = 4 , value = -1   Flip    results 3241
3 $a = 2 whilst $b = 3 , value = -1   Flip    results 2341
4 $a = 1 whilst $b = 4 , value = -1   Flip    results 2314
5 $a = 1 whilst $b = 3 , value = -1   Flip    results 2134
6 $a = 1 whilst $b = 2 , value = -1   Flip    results 1234

1,2,3,4

******************************

---- script -----

#!d:\perl\bin\perl.exe -w
use CGI::Carp qw(fatalsToBrowser);
print "content-type: text/plain\n\n";

## eg 1 ##

my @a = qw (1 2 3 4);
print "Unsorted nos => @a\n";
print '*'x 30,"\n";

my @b = sort spaceship @a;

sub spaceship {
    $counta++;
    print "$counta \$a = $a whilst \$b = $b , value \= ", $a<=>$b ;
    print "   Stay\n" if (($a<=>$b) == 1 || ($a<=>$b) == 0);
    print "   Flip\n" if (($a<=>$b) == -1);

    $a <=> $b;
};

print ((join ',',@b),"\n\n");
print (('*'x30),"\n");

## eg 2 ##

my @a = qw (4 3 2 1);
print "Unsorted nos => @a\n";
print '*'x 30,"\n";

my @b = sort spaceshipb @a;

sub spaceshipb {
    $countb++;
    print "$countb \$a = $a whilst \$b = $b , value \= ", $a<=>$b ;
    print "   Stay\n" if (($a<=>$b) == 1 || ($a<=>$b) == 0);
    print "   Flip\n" if (($a<=>$b) == -1);

    $a <=> $b;
};
print ((join ',',@b),"\n\n");
print (('*'x30),"\n");

## eg 3 ##

my @a = qw (3 4 2 1);
print "Unsorted nos => @a\n";
print '*'x 30,"\n";

my @b = sort spaceship3 @a;

sub spaceship3 {
    $count3++;
    print "$count3 \$a = $a whilst \$b = $b , value \= ", $a<=>$b ;
    print "   Stay\n" if (($a<=>$b) == 1 || ($a<=>$b) == 0);
    print "   Flip\n" if (($a<=>$b) == -1);

    $a <=> $b;
};
print ((join ',',@b),"\n\n");
print (('*'x30),"\n");


### end of msg & script ########
Thanks !

----- Original Message -----
From: "Charles K. Clarkson" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, November 12, 2001 4:54 PM
Subject: Re: [PBML] Re: explanation needed on sort using {$a<=>$b}


> "EternalLifeThereAfter" <[EMAIL PROTECTED]>
>
> : I have to agree that the spaceship operator is special ! But the
sequence
> of
> : sorting is still a very mysterious thing to me. I could imagine sorting
is
> : just like a factory conveyancy belt, pushing the elements thru some
> sorting
> : machine.
> : To find out what's exactly going on, I've decided to take a peep into
the
> : spaceship and this is what I got which I still do not understand
precisely
> : how it sorts. I would appreciate very much if someone could offer an
> : explanation on the undermentioned results.
> :
> : Thanks !
> :
> : @a = qw ( 12 34 14 0 35 44 2);
> : @b = sort spaceship @a;
> : print join ',', @b;
> : sub spaceship {
> :     print "\$a = $a whilst \$b = $b\n";
> :     $a<=>$b;
> : };
> :
>
>     Try this instead:
>
> my @a = qw ( 12 34 14 0 12 35 44 2);
> my @b = sort spaceship @a;
> print join ',', @b;
> sub spaceship {
>     my $comparison;
>     $comparison = 'lesser than';
>     $comparison = 'greater than' if $a > $b;
>     $comparison = 'equal to' if $a == $b;
>     print "\$a = $a whilst \$b = $b\n",
>             "\twhile \$a $comparison \$b ",
>             "spaceship returned ", $a <=> $b, "\n";
>     $a <=> $b;
> };
>
> __END__
>
>
>
> HTH,
> Charles K. Clarkson
> Clarkson Energy Homes, Inc.
>
> Money is not the root of all evil -- no money is.
>
>





_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to