Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by QuillMeantTen (Friar)
on Jan 07, 2016 at 10:07 UTC ( #1152157=note: print w/replies, xml ) Need Help??


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

Greetings,
First some suggestions, against using barewords, and for using open in its three terms form: more explicit and understandable
Also I put there use autodie because I'm lazy :$

use warnings; use strict; use autodie; open my $in,'<', "in.txt"; open my $out,'>', "out.txt";
Since you start at first line and then jump over each line to save to the previous line you can use the perl auto variable for line counts
my $previous_line; while (<$in>) { $current_line = $_; chomp $current_line; if(!defined($previous_line) || $. mod 2 != 0){ $previous_line = $current_line; next; } 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;
I'm not sure about the way you want to go over the file though, do you want to just jump over half of the lines?

  • 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 hdb (Monsignor) on Jan 07, 2016 at 10:13 UTC

    Why do you think the OP is jumping (wants to jump) over half of the lines?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1152157]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-05-29 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?