Now first thing is: where are $a and $b coming from? also not quite sure I understand how the number sorting works. The <=> operator from what I have read returns -1 0 or 1 depending whether the first number is smaller, equal or greater than the second one. So how does the comparison work? let's take the first 2 numbers: 1 and 2.

Please read the documentation perldoc -f sort. Your example is using the BLOCK LIST form. Braces {} surround the BLOCK. The LIST is the values of the unsorted array. The variables $a and $b are special global variables in perl. They are only used in block part of sort. The sort routine assigns values from the list to $a and $b. It executes the block to find out which should come first in the sorted list (This is done many times in a single run of sort)

The operators cmp and <=> are rarely used anywhere but in a sort block. They both return -1 to tell sort that $a belongs before $b, 0 if we do not care which is first, and 1 if $a belongs after $b. Your example is designed to demonstrate the difference between them. Cmp puts strings in order and <=> puts numbers in order. (cmp would but 10 before 2 because it puts the one before the two.) Good Luck, Bill

this will print -1 because 1 should be before 2 un a numerical sort. If you do it the other way arounf, it will print 1.

The main point is that the sort function expects a negative value is $a should be before $b, a positive value is $a should be after $b and a zero value if $a and $b should be considered equal. You can write your own block for specific comparisons, provided it return positive, zero and negative values as described above.

For example, you could rewrite

Code

my @number = sort { $a <=> $b } @unsorted;

as:

Code

my @number = sort { $a - $b } @unsorted;

and get the same results, since $a - $b will be negative if a < $b, nought if $a = $b and positive if $a > $b.

thanks guys. Needless to say, I am very happy to read the documentation, but I have no idea where to find, could you kindly let me know how to get to it? Sorry I am really really new to perl.

Also, to get back to the script: what it does in in theory clear, and it is better now that you added more info to it, but practically, I don't understand how many times this line

Code

my @number = sort { $a <=> $b } @unsorted;

runs. It must compare 2 numbers at each pass, so I assume it runs 4 times comparing respectively the 1st and 2nd, the 3rd and 4th, 5th and 6th, 7th and 8th number, or not? so comparison between 1 and 2 returns -1 because as you have said

Quote

a belongs before b

, then 11 and 24 returns -1 again and then what happens? 3 is smaller than 11 so does it get compared against it? thanks

Perl comes with extensive documentation and a tool (perldoc) to access it. To get directions for perldoc, at the command line of your computer, type:

Code

perldoc perldoc

I already gave you the command to access the documentation for sort.

Code

perldoc -f sort

The line containing the call to sort only runs once. However, the block portion of it is called many more than four times. It is not at all necessary to know how many.

Nor is it necessary to know how sort works. The documentation tells us that it uses the quicksort algorithm. You would have to consult a computer science text to find a description of that algorithm. The algorithm dictates which pairs of values must be compared. (Note: the algorithm does not work at all the way you seem to think it does)

All you have to do to use sort is to write a block that returns the right number (-1, 0, or +1) for any pair of values. Good Luck, Bill