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

--
http://fruiture.de

Replies are listed 'Best First'.
Re: Re: Comparing numbers
by virtualsue (Vicar) on Nov 29, 2002 at 11:32 UTC
     @index = ($i-1,$i);

    That's cute. My natural inclination would have been to keep just the index of the larger of the winning pair of consecutive numbers ($i). However, it costs very little to put the indices of both numbers aside and use them later to display the slice of interest from the array. :-)