I am little confused what it print for $a and $b, how the comparision is really happening. can you explain..... -
#!/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;
-
4 944
I am little confused what it print for $a and $b, how the comparision is really happening. can you explain..... -
#!/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
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.
The sort function documentation explains what goes on during the sort, here is what the sort functions sees internally: - @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: - 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.
The sort function documentation explains what goes on during the sort, here is what the sort functions sees internally: - @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: - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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;
}
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| |