Well, here we go with the final code.

I needed to get the thing working today, so I opted for the following way of doing it - a combination of in/elegance :) Viz:

sub Parse_SM_Sub_Header { # # [...private notes...] # # The array returned by this sub contains the following items: # # 0 Sub system # 1 Marriage state # 2 Marriage vote # 3 Split plan # 4 Split plan lock # 5 Link plan # 6 Link plan lock # 7 Split plan vote # 8 Link plan vote # 9 Cycle time lock # 10 Cycle time # 11 Rotation state # 12 Rotation amount # 13 Requested cycle time # 14 VF state # 15 Critical SA # 16 Critical SA DS # my ($line) = @_; my ($tmpbuf); my (@retarg); my %patterns = ( qr/SS\s*(\d+)([MF# ])([\+\- ])/ => sub { push(@retarg, $1); # Sub system push(@retarg, $2); # Marriage state push(@retarg, $3); # Marriage vote }, qr/PL\s*(\d+)([\#\.])(\d+)([\#\.]?)/ => sub { push(@retarg, $1); # Split plan $tmpbuf = $2; # Split plan lock $tmpbuf = "" if ($tmpbuf ne "#"); push(@retarg, $tmpbuf); push(@retarg, $3); # Link plan $tmpbuf = $4; # Link plan lock $tmpbuf = "" if ($tmpbuf ne "#"); push(@retarg, $tmpbuf); }, qr/PV\s*([a-z]?\d+)(\.)(\d+)/ => sub { push(@retarg, $1); # Split plan vote push(@retarg, $3); # Link plan vote }, qr/C[LT]\s*([\#\^]?)\s*(\d+)\s*([\+\-])(\d+)\s*/, => sub { push(@retarg, $1); # Cycle time lock push(@retarg, $2); # Cycle time push(@retarg, $3); # Rotation state push(@retarg, $4); # Rotation amount }, qr/RL\s*(\d+)([,'"]?)/ => sub { push(@retarg, $1); # Requested cycle time push(@retarg, $2); # VF state }, qr/SA\s+(\d+)/ => sub { push(@retarg, $1); }, # Critical SA qr/DS\s+(\d+)/ => sub { push(@retarg, $1); }, # Critical SA DS ); $_ = {}; @retarg = (); $line =~ s/^\s*//; LOOP: while (length($line)) { for my $k (keys %patterns) { if ($line =~ s/^$k(?:\s+|\z)//) { $patterns{$k}->(); next LOOP; } } warn "Parse_SM_Sub_Header: Error parsing input\n + \\$line\\\n"; $_->{leftover} = $line; last; } $_; return(@retarg); } # end Parse_SM_Sub_Header
It works quite happily on all the versions of outputs that I have to deal with, so I think we'll be right for a while :)

Again, many thanks for everyone's help.

In reply to Re: Parsing a Variable Format String by ozboomer
in thread Parsing a Variable Format String by ozboomer

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.