Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Adding counters for text replacement

by xyzzy (Pilgrim)
on Jul 08, 2012 at 21:13 UTC ( #980623=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2021-04-21 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?