in reply to subtracting two lines in file

With proper formatting, your question would've been comprehensible:

Hi Monks, I am new to perl, so my question can be stupid question. I have file like this:

1 3 5 2 7 6 3 10 7

I want to subtract 2nd column of each line from previous line's 2nd column. I want to have output like this:

1 3 5 2 4 6 3 3 4

Any help will be appreciated.


To solve your problem, just store the current value at the end of every iteration, so you still have it in the next iteration:

#!/usr/bin/perl use strict; use warnings; my $prev_2 = 0; while (<DATA>) { my ($c1, $c2, $c3) = split; printf "%3d %3d %3d\n", $c1, $c2-$prev_2, $c3; $prev_2 = $c2; } __DATA__ 1 3 5 2 7 6 3 10 7

Output:

1 3 5 2 4 6 3 3 7

(BTW, your desired output suggests that you want to subtract the previous value from the current one, not the other way round).