Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Edit lines from a file and replace multiple lines. -- basic english approach

by Discipulus (Canon)
on Mar 29, 2019 at 08:40 UTC ( [id://1231851]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Edit lines from a file and replace multiple lines. -- basic english approach
in thread Edit lines from a file and replace multiple lines.

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found