Hello Endurance,

> This doesn't work..

..not even compile ;) A saner indentation will help:

use strict; use warnings; my $line = ''; while (<>) { $line .= $_; if($line =~ /solar winds/) { { if( $line =~ /country/) { $line =~ s/country/place/; } } elsif($line =~ /solar power/) { { if( $line =~ /country/) { $line =~ s/country/place/; } } $line = ''; } __END__ syntax error at endurance01.pl line 16, near "elsif" syntax error at endurance01.pl line 23, near "}" Missing right curly or square bracket at endurance01.pl line 23, at en +d of line endurance01.pl had compilation errors.

You used too much curlys in your if statements. Then: you didnt follow my approach but instead you are adding to $line and so loosing any notion of the current line that is one of your requirements. As side note if you s/this/that/ is not needed the matching before if $x =~ /this/ because the sostitution only acts if it matches.

But let's try to implement (partially: is your work ;) my approach:

> Open the file named file.txt. Abort if the file is unreadable. Read the file line per line. If solar wind is found annotate it's line number as solar_linenum Print line just read anyway. Read and print two lines more. If the third line has country then replace it with place and print the line but also set solar_linenum to 0. The same for the other requirement.

use strict; use warnings; # Open the file named file.txt. Abort if the file is unreadable. my $filename = 'file.txt'; open my $file_handle, '<', $filename or die "Unable to open the file[$ +filename]!"; # a switch to hold if solar wind is found; my $solar_wind_found_at_line = undef; # Read the file line per line. while ( my $line = <$file_handle> ){ # remove the newline chomp $line; # print is your friend! is the first and powerful debug tool.. print "DEBUG: line $. -->$line<--\n"; # check for solar wind if ( $line =~ /solar winds/){ # annotate the current line number $solar_wind_found_at_line = $.; print "DEBUG: 'solar wind' found at line $.\n"; } # if ( $solar_wind_found_at_line IS DEFINED AND THE CURERENT +LINE IS THE THIRD AFTER SOLAR WINDS ){ # REPLACE COUNTRY WITH PLACE (THIS CAN HAPPEN OR NOT) # RESET $solar_wind_found_at_line TO UNDEF (THIS MUST HAPP +EN ANYWAY) # } # at the end the expected output print "OUTPUT DESIRED: [$line]\n\n" }

HtH

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re^3: Edit lines from a file and replace multiple lines. -- basic english approach by Discipulus
in thread Edit lines from a file and replace multiple lines. by Endurance

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.