O Ye Monks, I plea to be enlighten'd

I need to parse a set of files written by report programs. Each file has some sections delimited by before- and after- lines. Every section's contents will receive a different treatment. An example is given below. What I want to have is a *very* simple method to extend the parser, so as to account for future file formats. While future formats may have different section selector strategies, I want to have this pretty general case set down first.

---------------------------------------------------------------------- +----- @--- Callsites: 2 ---------------------------------------------------- +----- ---------------------------------------------------------------------- +----- ID Lev File/Address Line Parent_Funct MPI_Call 1 0 0x8048ad5 [unknown] Reduce 2 0 0x8048a3b [unknown] Bcast ---------------------------------------------------------------------- +----- @--- Aggregate Time (top twenty, descending, milliseconds) ----------- +----- ---------------------------------------------------------------------- +----- Call Site Time App% MPI% COV Bcast 2 9.5 24.71 65.75 0.59 Reduce 1 4.95 12.87 34.25 1.35 ---------------------------------------------------------------------- +-----

What I have devised up to now is something like the attached. That should parse the presented sample. Adding parsers should be a matter of writing proper RE for start and end lines, and then deciding what to do with each line belonging to the section. This parsing template would go in some config file or whatever.

As you can see, delimiter lines must not enter into process(). While my foreach loop may not be the most clever thing, it sorta works for me (suggestions gladly accepted).

#!/usr/bin/perl -w package Parser; use strict; sub new { bless {}; } sub add { my $self = shift; my $hash = shift; push @{$self->{parsers}}, $hash; } sub parse { my $self = shift; my $file = shift; open FILE,'<',$file || die "Can't open $file"; while(<FILE>) { foreach my $p (@{$self->{parsers}}) { if(/$p->{start}/ ... /$p->{end}/) { $p->{process}($_) unless (/$p->{start}/ || /$p->{end}/); } } } close FILE; } 1; sub parse1 { my $text = shift; print "Into parse1\n"; print $text; } sub parse2 { my $text = shift; print "Into parse2\n"; print $text; } my $p = Parser::new(); $p->add({ start => "^ ID Lev File/Address", end => "---", process => \&parse1}); $p->add({ start => "^Call *Site *Time *App", end => "---", process => \&parse2}); $p->parse($file);
Now, this works OK when only one parser is add()ed. However, with two parsers, I get the following output for the sample enclosed:
Into parse2 ID Lev File/Address Line Parent_Funct MPI_Call Into parse1 1 0 0x8048ad5 [unknown] Reduce Into parse2 1 0 0x8048ad5 [unknown] Reduce Into parse1 2 0 0x8048a3b [unknown] Bcast Into parse2 2 0 0x8048a3b [unknown] Bcast Into parse1 Bcast 2 20 35.85 64.45 0.76 Into parse2 Bcast 2 20 35.85 64.45 0.76 Into parse1 Reduce 1 11 19.78 35.55 1.31 Into parse2 Reduce 1 11 19.78 35.55 1.31

So:

Thank you very much in advance


In reply to Parsing text sections by betacentauri

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.