I have a feeling that I'm overlooking something obvious, but I've been away from Perl for about a year, and I'm having trouble getting the code below to work.

I'm trying to read through a system log file, and pull out the users that have entered orders on a patient. The way to tell that, however, is a two-line process...

I need to find the line that has a segment beginning with 'UB' (user identifier) directly following a pipe... but only when it's following a line (and not necessarily the next line) that has 'MSG-HDR' in it.

Here's what I've got so far:

$orderfound = 0; # Flag used to keep track of Orders being found while (<>) { $line = $_; if ($line=~/MSG-HDR/) { $orderfound = 1; } if (($orderfound) && ($line=~/|UB/)) { print "$line \n"; $orderfound = 0; } }

And here's a file snippet... (names changed, obviously)

*-X1AA1 ENTERED* C 6 18 374,032906 P 090003 090003 23-027 D,0 + 043800 VORHEES, JASON *-X1AA1 ENTERED* C 6 18 374,032906 P 090003 090003 43-044 D,0 + 045800 MYERS, MICHAEL *-X1AA1 ENTERED* C 6 18 373,032906 P 090003 090003 21-032 D,0 + 047000 KREUGER, FREDDY 0* * * * * * * * * * * * * * * * * * * * * * * * * * 36-060 090003 + *MSG-HDR D8020DE9 00000056 143CA421 007C0000 0329A008 84090003 + <01 |U E9C160 ||UBZ 0000 VORHEES, JASON RPH SJP ||UD 000 +0. M8||QA# 0000 ||QA= 11FF ||QB= 1130 Q0V| <02 |QB= 00C8 QA3| + <03 * * * * * * * * * * * * * * * * * * * * * * * * * * + *-Q1AA1 ENTERED* C 6 18 371,032906 P 090003 090003 36-060 D,0 + 046800 VORHEES, JASON

What's hapening right now is that I'm printing out the line that has 'MSG-HDR' in it, but I need it to print the line with 'UBZ 0000 VORHEES, JASON' in it.

I'm sure I'm missing something obvious... I just don't see it...


In reply to Matching over two lines by TrekNoid

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.