in reply to Re: Getting the next array element while still keeping the current one
in thread Getting the next array element while still keeping the current one
When I try to use $array[$i+1] - $array[$i] I get use of uninitialized value in subraction.
This is probably a sign that you run $i across all valid indexes in your array -- which means that $i + 1 will be one past the end of the array, and thus uninitialized. If you are looking for adjacent differences, try this:
my @array = ( ... ); my @adj_diff; for ( my $i = 0; $i < $#array; ++$i ) { push @adj_diff, $array[$i+1] - $array[$i]; }
This should result in @adj_diff having one fewer elements than @array. If you like the functional mode of programming, you could do it with a map too:
my @adj_diff = map { $array[$_+1] - $array[$_] } 0 .. $#array-1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^2: Getting the next array element while still keeping the current one
by hv (Prior) on May 02, 2004 at 12:28 UTC | |
by tkil (Monk) on May 02, 2004 at 18:07 UTC |