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


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.