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

In reply to Re^3: Need help to make correction in a perl script by ig
in thread Need help to make correction in a perl script by srsahu75

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.