http://qs321.pair.com?node_id=1214669


in reply to Abusing Map

First, there is an issue with your code, since $i can be the index of the last element, $i+1 can be the index of the element after the last. With warnings on, perl will complain about the use of an undef value.

Then, there may be a module to do what you want, but one possible way is to use the list of indexes as the input list rather than @a itself. With the range operator .. you can do it like: @b = map { $a[$_] - $a[$_+1] } 0..$#a-1;. Or, to save a few characters: @b = map { $a[$_-1] - $a[$_] } 1..$#a; (with $#array the last index of @array)