in reply to Need help to make correction in a perl script

Here is mine:
#!/usr/bin/perl use strict; use warnings; my $found_f = 0; my ($lastline, $line); open (INPUTFILE, "test.txt") or die "Unable to open test.txt: $!"; # 'or die' Added while (<INPUTFILE>) { chomp; if (/^[A-Z]{2}:\s/) { $lastline = $line; $line = $_; $found_f = 1; } elsif ($found_f) { s/^ {4}/ /; $line .= $_; } elsif (/^$/) { $lastline = ' '; $found_f =0; } } # Added $lastline .= $line; # Added - this was your main problem print "$lastline\n"; close (INPUTFILE); # Added
Notice how easier it is to read when you indent if and while statements? No wonder you missed the final }.
Update: Simplified second test (unnecessary RE) and removed next, which is also unnecessary.

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:13 UTC
    Thanks for the try but I require the output in this format. In my present script the lastline is not coming.
    DN: Data Name Aquatic Sciences TI: Title Distribution and abundance of bound volumes of the file in i +nstitute 1978 AU: Author Sahu, SR; Gawas, AKG AF: Affiliation Inst. of document scanning and documentation profiling + at library, toward the prograssive society and establishment SO: Source Test document file. Vol. 5, no. 1, pp. 1-14. Jul 1990. DE: Descriptors Article Subject Terms: Abundance; Ecological distribut +ion; Geographical distribution; Life cycle; Zooplankton; Article Taxo +nomic Terms: Euphausia; Article Geographic Terms: ISW, LA: Language English