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;
In reply to Re^2: Getting the next array element while still keeping the current one
by tkil
in thread Getting the next array element while still keeping the current one
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |