Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

In many real-world cases ... and yours is no exception ... an input file consists of various “lines” which have a recognizable and useful pattern.   Generally, some line marks the beginning of a group of records, another line marks the end, and the rest (which might have a less-recognized pattern) can simply be handled by an “everything else” case.   The awk tool was based on this simple notion, and Perl more-or-less evolved from it.   So, here is a general sketch of a good approach – extemporaneous and untested:

my @lines = []; # # THE MAIN PROGRAM READS THE FILE LINE-BY-LINE, CLASSIFYING THEM # USING REGULAR EXPRESSIONS # while(<>) { if (/^\<SUBBEGIN) { # STARTING LINE @lines = []; } elsif(/^<SUBEND/) { # ENDING LINE look_for_patterns(); } # INSERT "elsif" CASES HERE TO FILTER OUT BLANK LINES # OR OTHER UNWANTED "JUNK," IF APPLICABLE ... # else { # "EVERYTHING ELSE" push @lines, $_ } } look_for_patterns(); # IF APPROPRIATE – MAY NOT BE, FOR THIS CASE # THIS SUBROUTINE EXAMINES EACH ACCUMULATED GROUP OF LINES # LOOKING TO SEE IF BOTH DESIRED PATTERNS ARE INSIDE. sub look_for_patterns { my $CFU_seen = 0; my $CFB_seen = 0; foreach (@lines) { if (/CFU-TS10-ACT/) { $CFU_seen = 1; } elsif (/CFB-TS10-ACT/) { $CFB_seen = 1; } } if ($CFU_seen && $CFB_seen) print "Yay!\n"; }

The first block in this completely extemporaneous code example is a simple, awk-like loop which classifies lines.   The second subroutine then loops through the most-recent set of lines that have been accumulated.   (This subroutine is called each time an ending-record is found, and maybe also(!) at end-of-file.)

For what it’s worth, I no longer use awk to solve such problems, although a comparison between this and an awk solution may be instructive.   (There is, in fact, an a2p tool which directly translates “awk 2 perl.”)   But this is a time-tested and classic approach for dealing with a use-case which is so very common that it inspired the creation of at least two legendary and venerable software tools.

In reply to Re: Check multiple lines exist in a record by sundialsvc4
in thread Check multiple lines exist in a record by gbwien

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2020-01-28 03:35 GMT
    Find Nodes?
      Voting Booth?