G'day Mark.Allan,

Here's a solution that uses named capture buffers; as such, it requires Perl 5.10.0 or later.

#!/usr/bin/env perl use 5.010; use strict; use warnings; my $re = qr{ (?> ^TYPE \s+ (?<value>VALUE\d+) | ^CAUSE \s+ (?<cause>\w+) | ^[AE]FFECT \s+ (?<effect>\w+) ) }mx; { local $/ = "ENDTYPE\n"; while (<DATA>) { my %type = (value => '', cause => 'UNDEF', effect => 'UNDEF'); @type{keys %+} = values %+ while /$re/g; say "$type{value}:$type{cause},$type{effect}"; } } __DATA__ // HEADER TAG // VERSION TAG TYPE VALUE1 EQUALS MAIN I am useless text CAUSE FAIL AFFECT ERROR ENDTYPE TYPE VALUE2 EQUALS MAIN I am useful test ENDTYPE TYPE VALUE3 EQUALS MAIN CAUSE DEGRADED ENDTYPE TYPE VALUE4 EQUALS MAIN AFFECT WARNING ENDTYPE TYPE VALUE5 EQUALS MAIN This comes after TYPE VALUE4 Strange effect before cause EFFECT EFFECT_FIRST CAUSE CAUSE_SECOND ENDTYPE

Output:

$ pm_multiline_parse.pl VALUE1:FAIL,ERROR VALUE2:UNDEF,UNDEF VALUE3:DEGRADED,UNDEF VALUE4:UNDEF,WARNING VALUE5:CAUSE_SECOND,EFFECT_FIRST

Notes:

Finally, your sort will fail if your TYPEs exceed VALUE9. This short piece of code highlights the mistake you're making and how to fix it:

$ perl -Mstrict -Mwarnings -E ' my @x = qw{VALUE10 VALUE2 VALUE1}; say "*** Sorting Mistake ***"; say for sort @x; say "*** Sorting Correctly ***"; say for sort { substr($a, 5) <=> substr($b, 5) } @x; ' *** Sorting Mistake *** VALUE1 VALUE10 VALUE2 *** Sorting Correctly *** VALUE1 VALUE2 VALUE10

-- Ken


In reply to Re: File Parsing and Pattern Matching by kcott
in thread File Parsing and Pattern Matching by Mark.Allan

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.