in reply to Re: Need help to make correction in a perl script
in thread Need help to make correction in a perl script

Thanks for the try and suggestions. I can understand that since the last input $_ is not starting with ^A-Z{2}:\s hence it is not taking the last field. I am not able to visualize, how to do that. I need the out put like this:
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

Replies are listed 'Best First'.
Re^3: Need help to make correction in a perl script
by ig (Vicar) on Mar 20, 2009 at 18:50 UTC

    Like so many things, the solution is easy to understand once you know it but difficult to see when you don't know where to look.

    Your program reads the last two lines and copies them into your variable $line but it exits without ever printing the contents of this variable. All you have to do is print the contents of this variable after reading the last line of the file.

    Your loop terminates after reading the last line of the file. Therefore, to print the contents of $line after reading the last line of the file, you need to add a print statement after your loop.

    Your program is otherwise a bit more complex than it needs to be. Here is a simplified version, with a final print added, to print that troublesome last line.

    #!/usr/bin/perl -w use strict; use warnings; my $line; while (<DATA>) { chomp; if (/^$/) { print "\n"; $line = ""; } elsif (/^[A-Z]{2}:\s/) { print "$line\n" if($line); $line = $_; } else { $line .= " $_"; } } print "$line\n" if($line); __DATA__ DN: Data Name Information Sciences TI: Title Distribution and abundance of bound volumes of the file in institute shelves from 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 distribution; Geographical distribution; Life cycle; Zooplankton; Article Taxonomic Terms: Euphausia; Article Geographic Terms: ISW, LA: Language English
Re^3: Need help to make correction in a perl script
by gokuraku (Monk) on Mar 20, 2009 at 13:15 UTC
    What is not coming? Is there a blank line at the end of the file that is not printing or are you expecting a value of some kind to print at the end when the file has been output?