Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using Perl 5.10, you can do something very similar to what you have now but with a much nicer layout by using the given/when structure:</p> <pre><code>use 5.010; while (&lt;ERR_LOG&gt;) { chomp; given ($_) { when ( m{^&lt;parameter&gt;: (.*)}x ) { push @parameter, $1 } when ( m{^&lt;result&gt;: (.*)}x ) { push @result, $1 } when ( m{^&lt;stderr&gt;: (.*)}x ) { push @stderr, $1 } default { $stderr[-1] .= "\n$_" } } } </code></pre> <p>It's worth noting that for the default case here, rather than keeping a separate $err_msg variable, I'm simply pushing onto <code>@stderr</code> when I see a <code>stderr</code> tag, and appending to the last item of the <code>@stderr</code> array if I see a continuation line. I'm adding a newline when I see continuation lines, since I assume you want them preserved.</p> <p>Despite the above code looking quite elegant, I'm not <em>really</em> all that fond of keeping three separate arrays, since it will presumably cause you headaches if things get out of sync, and because if you want to add more fields in the future you'll end up with lots and lots of variables floating around that you'll need to keep track of. I'd suggest storing each record inside a hash, and then keeping an array of records:</p> <pre><code>use 5.010; my @records; my $prev_key; while (&lt;ERR_LOG&gt;) { chomp; given ($_) { when ( m{^&lt;parameter&gt; }x ) { push(@records, {}); continue; } when ( m{^&lt;(\w+)&gt;: (.*)}x ) { $records[-1]{$1} = $2; $prev_key = $1; } default { $records[-1]{$prev_key} .= "\n$_"; } } } </code></pre> <p>Here we're pushing a new record onto the array when we see a field, adding an entry to our hash whenever we see a key/value pair, and appending to the last field we added to if we see a continuation line. The end result of <code>@records</code> looks like this:</p> <pre><code>( { parameter =&gt; 'test_one_count', result =&gt; 0, stderr =&gt; qq{Expected "test_one_count=2" and the actual value is 0\ntest_one_count=0}, }, { parameter =&gt; 'test_two_count', result =&gt; 4, stderr =&gt; qq{Expected "test_two_count=2" and the actual value is 4\ntest_two_count=4}, } ) </code></pre> <p>Now you can pass just a single data structure around which contains all of your records, and you can add more fields in the future (even multi-line ones) and they'll be correctly handled.</p> <p>If you're not using Perl 5.10, then this may be a good excuse to upgrade. If not, you can translate the given/when structures into more traditional if/elsif/else structures, but they lose much of their beauty in the conversion.</p> <p><em>Paul</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload