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.
|