Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Abusing Map

by Eily (Prior)
on May 16, 2018 at 12: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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-11-13 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (149 votes). Check out past polls.

    Notices?