in reply to How to loop over two lines, alter a value in the current line and save it to the previous line?

Hello, adapting your code:

#!/usr/bin/perl use strict; use warnings; my $previous_line = <DATA>; while ( my $current_line = <DATA> ) { chomp for ( $previous_line, $current_line ); my @previous_cols = split ' ', $previous_line; my @current_cols = split ' ', $current_line; print "$current_cols[0]: $current_cols[1] "; $current_cols[1] += $previous_cols[1]; print "now changed to $current_cols[1]\n"; $previous_line = join ' ', @current_cols; } __DATA__ seed 1 foo 1 bar 2 baz 3 qux 4
Output:
foo: 1 now changed to 2 bar: 2 now changed to 4 baz: 3 now changed to 7 qux: 4 now changed to 11

You may want to look at Text::CSV_XS for this kind of task.

Hope this helps!

The way forward always starts with a minimal test.
  • Comment on Re: How to loop over two lines, alter a value in the current line and save it to the previous line?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to loop over two lines, alter a value in the current line and save it to the previous line?
by rjc33 (Sexton) on Jan 07, 2016 at 11:32 UTC
    Hi, thanks a lot for your answer, I'll have a look at Text::CSV XS. I must admit this is slightly over my head, I've replaced $previous_line = $current_line with:
    $current_columns[2] += $previous_columns[2]; $previous_line = join ' ', @current_columns;
    This cumulates the 3rd column, which although I mentioned it in the question on reflection isn't actually what I was wanting to do. Apologies for wasting your time with a badly worded question. I'm looking to alter a value in the current line, and then have that value saved to the previous line for use in the next iteration of the loop.

      Apologies for wasting your time with a badly worded question.

      You may have an XY problem. Might be a good time to state what it is you are really trying to accomplish. Could be that this thing with using the previous line is not the best approach.

      I'm looking to alter a value in the current line, and then have that value saved to the previous line for use in the next iteration of the loop.

      ??

      The code you posted does exactly that. Maybe this will show it more clearly?

      #!/usr/bin/perl use strict; use warnings; my $previous_line = <DATA>; print " prev\tcurrent\n"; while ( my $current_line = <DATA> ) { chomp for ( $previous_line, $current_line ); print "input:\t$previous_line\t$current_line\n"; my @previous_cols = split ' ', $previous_line; my @current_cols = split ' ', $current_line; $current_cols[1] += $previous_cols[1]; $previous_line = $current_line = join ' ', @current_cols; print "output:\t$previous_line\t$current_line\n"; print "\n"; } __DATA__ nul 0 foo 1 bar 2 baz 3 qux 4
      Output:
      prev current input: nul 0 foo 1 output: foo 1 foo 1 input: foo 1 bar 2 output: bar 3 bar 3 input: bar 3 baz 3 output: baz 6 baz 6 input: baz 6 qux 4 output: qux 10 qux 10

      update: reworked example code to not use a sub


      The way forward always starts with a minimal test.
        Hi, thanks for all your help. I just initially misused the word "cumulate", I'm trying to do exactly what you just quoted in italics but my current code falls just short. I want to alter the value in the 3rd column of each line based on some calculations using values from the same line and the previous line (so that has to be included somehow, although I'm open to completely different ways of doing it). So I want my loop to start on line 2, altering the 3rd column based on other values in line 2 and line 1; the next iteration would then alter the 3rd column of line 3 based on values in line 3 and line 2, and so on. My current code does this, the problem is that whatever is the $previous_line at the time contains the initial values from the file itself, I want it to contain the 3rd column value that I changed in the previous iteration of the loop.

        So for example, if in the first 3 lines of the file my 3rd columns are 4, 6, 9. My loop will start on line 2, and say it will change 6 to 8. The next iteration will be on line 3, however when it accesses line 2 it uses the old value 6 when I need it to use the new value 8, and hence from the third line onwards my code gives me incorrect output. So I'm just looking for a way to save the changed value to the previous line for the next iteration of the loop.