Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Abusing Map

by Eily (Monsignor)
on May 16, 2018 at 16:50 UTC ( #1214669=note: print w/replies, xml ) Need Help??

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)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214669]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2020-12-01 00:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found