Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Adding counters for text replacement

by xyzzy (Pilgrim)
on Jul 08, 2012 at 21:13 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://980623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-20 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found