http://www.perlmonks.org?node_id=980623


in reply to Adding counters for text replacement

If you want to increment the value as each substitution is made, consider using the /e modifier for your substitution regex, which evals the replacement text before making the substitution. Something like this might work, but only if you have a matching number of opening and closing tags, and only if none of them are nested

my $bpt_counter=0; my $ept_counter=0; for ($YOUR_ENTIRE_TEXT_IN_ONE_STRING) { s/(&lt;p&gt;)/++$bpt_counter;qq(<bpt i="$bpt_counter" x="$bpt_counte +r">$1</bpt>)/eg; s|(&lt;/p&gt;)|++$ept_counter;qq(<ept i="$ept_counter">$1</ept>)|eg; } #just to be safe warn "Uh-oh!\n" unless $bpt_counter == $ept_counter;

$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.