in reply to Re: extracting corresponding values from another array
in thread extracting corresponding values from another array

i am still having problems with this................. say ;
my $numbers = ('0.12', '0.35','1.34');
the code below perfectly calculates the greatest difference between two numbers, but i can't get it to return only the two numbers between whcih the difference lies. Any ideas how I could do this? thanks again. e.g. output
the diff between 0.35 and 1.34 is 0.99.
for ($i = 2; $i < @numbers; $i++) { if (defined ($numbers[$i]) && defined ($numbers [$i-1])) { $d = abs ($numbers [$i] - $numbers [$i-1]); $d =~ s/-//g; $diff = $d if $diff < $d; @diff_val = "$numbers[$i]\t$numbers[$i-1]\n"; @diff_val = split ('', $testing); push (@d, $d, $diff_val[0], $diff_val[1]); } } print "Greatest difference in the first sample is $diff\n";

Replies are listed 'Best First'.
Re: extracting corresponding values from another array
by Abigail-II (Bishop) on Dec 03, 2002 at 17:05 UTC
    my @numbers = qw /0.12 0.35 1.34/; my $diff = -1; my $di = 0; for (my $i = 1; $i < @numbers; $i ++) { my $d = abs ($numbers [$i] - $numbers [$i - 1]); if ($d > $diff) { $diff = $d; $di = $i; } } if ($di) { print "The diff between $numbers[$di - 1] and $numbers[$di] is $di +ff.\n" }

    No time to comment, Amsterdam.pm meets in 10 minutes.

    Abigail