in reply to Restarting counters in text
but because the variable to match is $_, this is the default variable for the match operator and it is therefore sufficient to write:if ( $variable !~ /^<exm>/ ) { $exmno = 0 );
or even shorter isif ( !/^<exm>/ ) { $exmno = 0 };
Two further points are that 1) this test is anyway redundant because s// returns false for non-match and can be used directly to drive the reset to zero and 2) if the counter is zero until it encounters an exm line, and if the first exm line in a group is number 1, then the counter needs to be pre- rather than post-incremented, e.g.:/^<exm>/ or $exmno = 0;
my $exmno = 0; while(<>) { s/^<exm>/'<exm num ='.++$exmno.'>'/ or $exmno = 0; print $_; }
-M
Free your mind
|
|---|