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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.