in reply to Sort the array

First, code tags are  <code> and </code> not  [code] and [/code].

For a numerical sort you want to use a numerical comparison operator, for example:

my @theList = ( 205, 4, 100, 1, 2, 2000, 6, 93, 2, 65 ); my @theSortedList = sort { $a <=> $b } @theList; print join( ',', @theSortedList ), "\n";

Replies are listed 'Best First'.
Re^2: Sort the array
by shekarkcb (Beadle) on Nov 10, 2008 at 11:37 UTC
    Hi,

    your solution works fine. But Can you explain the line a but more ,

    my @theSortedList = sort { $a <=> $b } @theList;

    Since i m new to Perl, i did not understood what its actually doing.
    Thanks,
    Shekar

       <=> is a numeric equality operator.   See perlop for details.    $main::a and  $main::b are special variables that sort uses internally.   So the code block is a call-back subroutine that sort uses to compare the values of the incoming list.