By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

Sort function

P: 89
I am little confused what it print for $a and $b, how the comparision is really happening. can you explain.....
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. @array = qw(7 8 59 58 4 5 6 2 59);
  4.  
  5. @array2 = sort {$a <=> $b; print "$a and $b.... comp.. \n"; } @array;
  6.  
  7.  
  8. print "$_\n" for @array2;
  9.  
Feb 25 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
I am little confused what it print for $a and $b, how the comparision is really happening. can you explain.....
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. @array = qw(7 8 59 58 4 5 6 2 59);
  4.  
  5. @array2 = sort {$a <=> $b; print "$a and $b.... comp.. \n"; } @array;
  6.  
  7.  
  8. print "$_\n" for @array2;
  9.  
Here is a link that doesn't do a bad job explaining how sort works. I would read it.


Also, I would read the sort page on perl doc, because you cannot have that print function in the sort. Only the comparison pieces can be in there.

Regards,

Jeff
Feb 25 '08 #2

P: 89
Here is a link that doesn't do a bad job explaining how sort works. I would read it.


Also, I would read the sort page on perl doc, because you cannot have that print function in the sort. Only the comparison pieces can be in there.

Regards,

Jeff
thanks , i need only the page from where you find it.
Feb 25 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
The sort function documentation explains what goes on during the sort, here is what the sort functions sees internally:

Expand|Select|Wrap|Line Numbers
  1. @array = qw(7 8 8 59 58 4 5 6 2 59);
  2. for my $i (0..$#array) {
  3.     print "$array[$i] <=> $array[$i+1] = ", $array[$i] <=> $array[$i+1],"\n";
  4. }
output:

Expand|Select|Wrap|Line Numbers
  1. 7 <=> 8 = -1
  2. 8 <=> 8 = 0
  3. 8 <=> 59 = -1
  4. 59 <=> 58 = 1
  5. 58 <=> 4 = 1
  6. 4 <=> 5 = -1
  7. 5 <=> 6 = -1
  8. 6 <=> 2 = 1
  9. 2 <=> 59 = -1
  10. 59 <=>  = 1
-1 means the left operand is less than the right
0 means they are equal
1 means the left operand is greater than the right

the "cmp" and "<=>" operators both work like this. But "cmp" sorts in ASCII order instead of numeric order.

And thats how perl sorts them internally but the comparison is done as many times as needed untill all the data is correctly sorted.
Feb 25 '08 #4

P: 89
The sort function documentation explains what goes on during the sort, here is what the sort functions sees internally:

Expand|Select|Wrap|Line Numbers
  1. @array = qw(7 8 8 59 58 4 5 6 2 59);
  2. for my $i (0..$#array) {
  3.     print "$array[$i] <=> $array[$i+1] = ", $array[$i] <=> $array[$i+1],"\n";
  4. }
output:

Expand|Select|Wrap|Line Numbers
  1. 7 <=> 8 = -1
  2. 8 <=> 8 = 0
  3. 8 <=> 59 = -1
  4. 59 <=> 58 = 1
  5. 58 <=> 4 = 1
  6. 4 <=> 5 = -1
  7. 5 <=> 6 = -1
  8. 6 <=> 2 = 1
  9. 2 <=> 59 = -1
  10. 59 <=>  = 1
-1 means the left operand is less than the right
0 means they are equal
1 means the left operand is greater than the right

the "cmp" and "<=>" operators both work like this. But "cmp" sorts in ASCII order instead of numeric order.

And thats how perl sorts them internally but the comparison is done as many times as needed untill all the data is correctly sorted.
Thanks, Yes this is the algorithim which repet in sort function a number of ime unless and untill the whole array is in a sorted manner.
Feb 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.