Here is a simple demonstration that Perl does indeed have a "post-condition loop construct" (contrary to the claim nearby in this thread), and that it is useful. Here is a trivial refactoring of the above code to make use of that. I don't repeat the input nor output, but if you provide it with the former, then it will duplicate the latter.

#!/usr/bin/perl -lw use strict; while( <DATA> ) { chomp; if( ! m[TIMINGCHECK] ) { print; next; } my $count= tr[(][(] - tr[)][)]; do { s[^][//]; print; last # Leaves the while( <DATA> ) loop if ! defined( $_= <DATA> ); chomp; $count += tr[(][(] - tr[)][)]; } while( 0 <= $count ); print; }

Note that the last jumps out of both "loops", which can be considered an improvement since the original code would warn about trying to print an undefined value (when given incomplete input).

And, here is one way I might refactor this (in order to avoid repeating the code used to read input and the code to count parens):

#!/usr/bin/perl -lw use strict; my $count= -1; while( <DATA> ) { chomp; $count= 0 if m[TIMINGCHECK]; $count += tr[(][(] - tr[)][)] if 0 <= $count; s[^][//] if 0 <= $count; print; }

Which also duplicates the above output, though it might not always agree on all inputs (it doesn't warn on incomplete input, certainly).

- tye        


In reply to Re^2: Comment a block that match a keyword (did) by tye
in thread Comment a block that match a keyword by yorkwu

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.