Re: perl cumulative module question



Rob Dixon wrote:

Your arithmetic is odd. You say

9-4 = 2

and so your resulting list should be (2,4,2). If you want to subtract the
previous result from the next item in your data list each time then this will do
the job.

HTH,

Rob


use strict;
use warnings;

my @data = (2, 6, 9);
my @delta = @data;
my $prev = 0;

$prev = $_ = $_-$prev for @delta;

print "@delta\n";

**OUTPUT**

2 4 5

Jay's solution is very similar but preferable to mine.

Rob
.



Relevant Pages

  • Re: How many 3-digit positive integrs are odd and do not contain the digit "5"?
    ... > I was thinking to get all the possible numbers and then subtract the ... by inclusion-exclusion, the total number that have a 5 or are odd is: ... Prev by Date: ...
    (sci.math)
  • Re: Countdown
    ... > add, shift left, shift right, and "check the bit you've just shifted ... checking for odd and even. ... biggest binary number that can be subtract from it and then repeat this ...
    (uk.media.radio.archers)
  • Re: Proof...
    ... > Repeat until n is 1. ... > if n is odd then subtract 1 from n ... > if n is even then divide n by 2 ...
    (sci.math)
  • Re: perl cumulative module question
    ... Your arithmetic is odd. ... If you want to subtract the ... To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx ... For additional commands, e-mail: beginners-help@xxxxxxxx ...
    (perl.beginners)
  • Re: Proof...
    ... > Repeat until n is 1. ... > if n is odd then subtract 1 from n ... > if n is even then divide n by 2 ...
    (sci.math)