in reply to Comparing numbers
Your code won't work for several reasons: $file is a scalar and in the context of your foreach-loop it is a list of one element. $number will be a lexical alias for $file while the single iteration of the loop happens. $number_counter will be increased once and $difference will be assigned 1, for a-(a-1)==a-a+1==1 .
Try this:
my @number = qw(3.34 3.67 4.75 4.98); my $maxdiff = 0; my @index = (); foreach my $i ( 1 .. @number-1 ){ my $diff = abs( $number[$i] - $number[$i-1] ); if( $diff > $maxdiff ){ $maxdiff = $diff; @index = ($i-1,$i); } } if( @index ){ printf "%f: %f , %f\n", $maxdiff, @number[ @index ] }
update: of course, see perlsyn and perldata
--
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Comparing numbers
by virtualsue (Vicar) on Nov 29, 2002 at 11:32 UTC |