On Mon, Jul 04, 2005 at 08:34:26PM +0000 Vineet Pande wrote: > Hello! > In the following code: > > ************************************************** > #!/usr/bin/perl > use warnings; > use strict; > my @unsorted = (1, 2, 11, 24, 3, 36, 40, 4); > my @number = sort { $a <=> $b } @unsorted; > print "Numeric sort: @number\n"; > ************************************************** > returns 1 2 3 4 11 24 36 40 > > what I don't understand is how the block functions here: we did not define > $a and $b? Also I donot understand this kind of block usage. > > Any comments appreciated!
in Perl literacy course lecture #5 Sorting and Perl Shlomo Yona <[EMAIL PROTECTED]> http://cs.haifa.ac.il/~shlomo/ ---------------------------------------------------------------------------- $a and $b $a and $b are two elements of the list being compared during some iteration. To optimize the calling of the sortsub, Perl bypasses the usual passing of arguments via @_, using instead a more efficient special-purpose method. Within the sortsub, the special package global variables $a and $b are aliases for the two operands being compared. The sortsub must return a number less than 0, equal to 0, or greater than 0, depending on the result of comparing the sortkeys of $a and $b. ----------------------------------------------------------------------------- hth -- GĂ©rard -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>