Below I made two small changes that may do what you want. If not, you've been given all the concepts; try yourself!

use strict; use warnings; my $file = "z.txt"; open my $fh, "<", $file or die "Unable to open $file: $!"; my ($p_val, $m_val); { local $/ = '\n\n\n'; while (<$fh>) { foreach (split/\n/) { if (s/\bProcessing\s\d+\.tx\.\d+: //) { print "$_\n"; } elsif (s/\bPhrase: //) { s/"//g; $p_val = $_; } elsif (/^\s\s(.+)/) { $m_val = $_; if (defined $p_val) { print "\t$p_val\t$m_val\n"; undef $p_val; ### moved undef here } else { ### new else condition print "\t\t$m_val\n"; } } } } } close $fh; __END__ Pulmonary embolism at the time of hip replacement Pulmonary embolism 1000 D0076131:PULMONARY EMBOLISM Dis +ease or Syndrome of hip replacement 1000 D0554893:HIP REPLACEMENT (STATU +S POST HIP REPLACEMENT) Finding of hemorrhage 1000 D0046004:HAEMORRHAGE (BLEEDING) Finding 1000 D0046011:HAEMORRHAGE NOT OTHERWISE SPECIFIED (H +EMORRHAGE NOT OTHERWISE SPECIFIED) Finding

In reply to Re^6: tab delimited extraction, formatting the output by hbm
in thread tab delimited extraction, formatting the output by zzgulu

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.