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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |