Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-03-28 15:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found