455,852 Members | 1,410 Online
+ 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 #!/usr/bin/perl   @array = qw(7 8 59 58 4 5 6 2 59);   @array2 = sort {\$a <=> \$b; print "\$a and \$b.... comp.. \n"; } @array;     print "\$_\n" for @array2;   Feb 25 '08 #1
4 Replies

 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 #!/usr/bin/perl   @array = qw(7 8 59 58 4 5 6 2 59);   @array2 = sort {\$a <=> \$b; print "\$a and \$b.... comp.. \n"; } @array;     print "\$_\n" for @array2;   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

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