Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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:32 UTC ( [id://1152164]=note: print w/replies, xml ) Need Help??


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

Hi, thanks a lot for your answer, I'll have a look at Text::CSV XS. I must admit this is slightly over my head, I've replaced $previous_line = $current_line with:
$current_columns[2] += $previous_columns[2]; $previous_line = join ' ', @current_columns;
This cumulates the 3rd column, which although I mentioned it in the question on reflection isn't actually what I was wanting to do. Apologies for wasting your time with a badly worded question. 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.
  • Comment on Re^2: How to loop over two lines, alter a value in the current line and save it to the previous line?
  • Download Code

Replies are listed 'Best First'.
Re^3: How to loop over two lines, alter a value in the current line and save it to the previous line?
by 1nickt (Canon) on Jan 07, 2016 at 12:43 UTC

    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.
      Hi, thanks for all your help. I just initially misused the word "cumulate", I'm trying to do exactly what you just quoted in italics but my current code falls just short. I want to alter the value in the 3rd column of each line based on some calculations using values from the same line and the previous line (so that has to be included somehow, although I'm open to completely different ways of doing it). So I want my loop to start on line 2, altering the 3rd column based on other values in line 2 and line 1; the next iteration would then alter the 3rd column of line 3 based on values in line 3 and line 2, and so on. My current code does this, the problem is that whatever is the $previous_line at the time contains the initial values from the file itself, I want it to contain the 3rd column value that I changed in the previous iteration of the loop.

      So for example, if in the first 3 lines of the file my 3rd columns are 4, 6, 9. My loop will start on line 2, and say it will change 6 to 8. The next iteration will be on line 3, however when it accesses line 2 it uses the old value 6 when I need it to use the new value 8, and hence from the third line onwards my code gives me incorrect output. So I'm just looking for a way to save the changed value to the previous line for the next iteration of the loop.

        How does this not accomplish that?
        $previous_line = $current_line = join ' ', @current_cols;
        The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-26 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found