http://qs321.pair.com?node_id=1152155

rjc33 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I’m pretty new to perl and coding in general, so apologies if this question has been dealt with before (I couldn’t find a solution anywhere obvious). I have a file that consists of 7 columns and many lines, and I need to alter the value of the 3rd column by performing a range of calculations involving other columns on the same line and also the previous line. I can do this, my problem is that I want the new value for the 3rd column of the current line to be saved as the loop proceeds and the current line becomes the previous line (effectively I want the values in the 3rd column to cumulate as the loop goes over each line). This is what I have so far (the middle part can be ignored as that's just the specific alterations to the value):

use warnings; use strict; open (IN, "in.txt") or die; open (OUT, "out.txt") or die; my $previous_line = <IN>; while (my $current_line = <IN>) { chomp $current_line; chomp $previous_line; my @previous_columns = split(" ", $previous_line); my $end_coor = $previous_columns[2] + $previous_columns[3]; my @current_columns = split(" ", $current_line); my $seq_length = length $previous_columns[6]; my $gap_count = $previous_columns[6] =~ tr/Q//; my $original_length = $seq_length - $gap_count; my $original_end_coor = $previous_columns[2] + $original_length; my $distance = $current_columns[2] - $original_end_coor; $current_columns[2] = $end_coor + $distance; $previous_line = $current_line; print OUT "$current_columns[2]\n"; } close IN; close OUT;

The important lines are the last 3 of the loop, the value of the 3rd column is changed and subsequently printed but when the loop finishes this new value is not passed to the previous line, which instead contains the original value for that column and line. Anyone know how I would change this? Many thanks