473,388 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Sort function

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
4 944
numberwhun
3,509 Expert Mod 2GB
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
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
4,059 Expert 2GB
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
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

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

Similar topics

18
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn,...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
4
by: Seeker | last post by:
Hi, I have an array of objects. My object definition is given below: function tempArray(code,height,weight) { this.code = code; this.height = height; this.weight = weight; }
7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
5
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
18
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.