in reply to storage of numbers

If you only need the min and the max, then you don't need to store your numbers and an array and then sort the array: just maintain a $min and $max variables as you read the file. Possibly something like this:

my ($min, $max) = (0, 0); while(<>){ chomp($input=$_); if($input ne ''){ $min = $input if $input < $min; $max = $input if $input > $max; } else{last;} } print "Min and Max are : $min $max \n";
EDIT: fixed a typo on the $max variable name.

Replies are listed 'Best First'.
Re^2: storage of numbers
by BillKSmith (Monsignor) on Oct 17, 2013 at 02:53 UTC
    Laurent, use strict; would have caught your typo. I prefer to get the special cases out of the way early.
    use strict; use warnings; my ($min, $max) = (0, 0); while(<DATA>){ chomp( my $input = $_ ); last if $input eq ''; $min = $input if $input < $min; $max = $input if $input > $max; } print "Min and Max are : $min $max \n"; __DATA__ 11 13 56 75 53 68 89 22
    Bill

      Laurent, use strict; would have caught your typo.

      Not really, Bill, since I did not try to run or even to compile the code. In my view, this was just a small code snippet showing changes compared to the original program, not a full program. But sure, I always use strict and warnings when I write actual programs. Thanks you for mentioning the typo.