On Sat, Nov 22, 2008 at 10:28 AM, ben perl <[EMAIL PROTECTED]> wrote:
> Is there a perl module to find cumulative in a column? It should subtract
> from the previous row and creates new column.
>
> For example, If i have the follow column in my file
>
> 2
> 6
> 9
>
> It gives me
>
> 2
> 6-2 = 4
> 9-4 = 2
>
> So the resulting column is
>
> 2
> 4
> 2
>
> Hope I am using cumulative as the right word to explain this.
>
> I could write my own script but did not want to reinvent the wheel.
>

It looks like what you want is a running total with subtraction.

Perl doesn't have any internal sens of "rows" and "columns," but it
does have a number of operators and function to iterate over lists and
arrays.

Assuming the the result you want is really "2,4,5" (9-4 is 5, not 2)
just use map():

   my $cur = 0;
   my @running = map { $cur = $_ - $cur } (2,6,9);

HTH,

-- jay

--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to