Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: 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:02 UTC ( [id://1152156]=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?

I am not sure what your question really is. You are modifying @current_columns but you are keeping $current_line unmodified and store it in $previous_line for further processing. Do you want your modifications reflected in $previous_line in the next iteration of your loop? If that is the question, you should really store the array @current_columns instead of $current_line, e.g.

my $previous_line = <IN>; chomp $previous_line; my @previous_columns = split(" ", $previous_line); while (my $current_line = <IN>) { chomp $current_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; print OUT "$current_columns[2]\n"; @previous_columns = @current_columns; }

Not tested...

  • 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 rjc33 (Sexton) on Jan 07, 2016 at 11:02 UTC
    Hi, thanks for your very quick answer. Yes, I would like my modifications reflected in $previous_line in the next iteration of the loop, so your suggestion makes a lot of sense. Unfortunately that change seems to make $previous_line always equal the first line of the file for some reason

      Correct, but it is not used anymore at all within the loop.

        I'm afraid I don't understand, by $previous_line always being the first line it affects all of the variables in the loop. The loop starts on line 2, with the previous line being line 1; in the next iteration I'd like it to be on line 3, with the previous line being line 2 (and so on).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found