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?
Output:#!/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
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
In reply to Re^3: How to loop over two lines, alter a value in the current line and save it to the previous line?
by 1nickt
in thread How to loop over two lines, alter a value in the current line and save it to the previous line?
by rjc33
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |