The following code builds a string containing the current transaction and either prints it or throws it away depending on the state of a keep flag.

use strict; use warnings; my $numbers = join '|', qw(2345678923 2121212121 4424352424 2323232323 +); my $transaction = ''; my $keep; my $startRe = qr!<BEGIN Transaction>!; my $endRe = qr!</END Transaction>!; my $match = qr!(^|\D)($numbers)($|\D)!; while (defined (my $line = <DATA>)) { my $inTransaction = $line =~ $startRe .. $line =~ $endRe; next unless $inTransaction || $keep; $transaction .= $line; $keep ||= $line =~ /$match/sm; next unless $inTransaction =~ 'E0$'; print $transaction if $keep; $transaction = ''; $keep = undef; } __DATA__ <BEGIN Transaction> 2345678923 </END Transaction> <BEGIN Transaction> 1 </END Transaction> junk <BEGIN Transaction> 12345678923 </END Transaction> <BEGIN Transaction> 2121212121 </END Transaction>

Prints:

<BEGIN Transaction> 2345678923 </END Transaction> <BEGIN Transaction> 2121212121 </END Transaction>

Note the use of the flip-flop operator (..) to keep track of inside/outside the transaction and to detect the last line of the transaction.

Note also the use of qr to precompile the regular expressions that are used for matching transaction start/end and (more importantly) to match the numbers.


Perl's payment curve coincides with its learning curve.

In reply to Re: Match a list of numbers from a list of files by GrandFather
in thread Match a list of numbers from a list of files by shawshankred

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.