Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: 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 14:00 UTC ( [id://1152185]=note: print w/replies, xml ) Need Help??


in reply to Re^3: 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 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.

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

Replies are listed 'Best First'.
Re^5: 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 14:07 UTC
    How does this not accomplish that?
    $previous_line = $current_line = join ' ', @current_cols;
    The way forward always starts with a minimal test.
      I've no idea why, but as with the first answer in this thread it seems to make $previous_line always be the first line in the file.
        Correction: I just hadn't implemented it properly, that works! Thanks so much! The issue was there was some circularity in my earlier code, part of the calculations I was doing were actually working on the old values (even though I wanted it to use new values on the whole), so once I took those out I could see that what you'd suggested works. Thanks again

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found