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 HAPPEN ANYWAY) # } # at the end the expected output print "OUTPUT DESIRED: [$line]\n\n" }