in reply to Need help to make correction in a perl script
I have used perltidy to improve the layout of your script, added a missing curly bracket at the end and added strict and warnings, which you should always do yourself, unless you have a very good reason not to. You can read more about these options in Use strict and warnings.
Here is your modified code:
#!/usr/bin/perl -w use strict; use warnings; my $found_f = 0; my ( $lastline, $line ); open( INPUTFILE, "test.txt" ); while (<INPUTFILE>) { chomp; if (/^[A-Z]{2}:\s/) { $lastline = $line; $line = $_; $found_f = 1; } elsif ( !/^[A-Z]{2}:\s/ && $found_f ) { s/^ {4}/ /; $line .= $_; next; } elsif (/^$/) { $lastline = ' '; $found_f = 0; } print "$lastline\n"; }
To understand why the LA field is not being printed, you need to think about what happens in the last iteration through your loop. When the last line of your data is read, which branch of your if statement is taken? What happens in that branch? After reading the last line of the file, is your loop block executed again? If not, how can the last value of $lastline be printed?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need help to make correction in a perl script
by srsahu75 (Initiate) on Mar 20, 2009 at 11:18 UTC | |
by ig (Vicar) on Mar 20, 2009 at 18:50 UTC | |
by gokuraku (Monk) on Mar 20, 2009 at 13:15 UTC |