Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: sequential substitutions

by hdb (Monsignor)
on Aug 03, 2018 at 11:06 UTC ( #1219800=note: print w/replies, xml ) Need Help??


in reply to Re^2: sequential substitutions
in thread sequential substitutions

Like this?

use strict; use warnings; my $i = 0; while(<DATA>){ s/<foo>.*?<\/foo>/"<foo>".++$i."<\/foo>"/e; print; } __DATA__ <foo>3</foo> <bar>a</bar> <foo>14</foo> <bar>bc</bar> <foo>159</foo> <bar>def</bar>

Replies are listed 'Best First'.
Re^4: sequential substitutions
by Anonymous Monk on Aug 03, 2018 at 11:56 UTC

    Basically, yes, though I am going to be doing it all at once (using a technique described in a later answer) - reading in the entire file, and then processing it with:

    $fragment =~ s/<foo>\d+<\/foo>/'<foo>' . $i++ . '<\/foo>'/eg;

    Thank you.

      All that  foo foo foo stuff... That's a lot of foo-ing around for a simple substitution — and a lot of room for error to creep in. Another way (needs Perl version 5.10+ regex extensions):

      c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $s = qq{www<foo> 3 </foo>\n<bar>999</bar>\n<foo>14 </foo>xx\n} . qq{<bar>998</bar>\nyyy<foo> 159</foo>z\n<bar>997</bar>\n} ; print qq{[[$s]]}; ;; { my $n = 1; $s =~ s{ <(foo)> \s* \K \d+ (?= \s* </\g-1>) }{ $n++ }xmsge; } print qq{[[$s]]}; " [[www<foo> 3 </foo> <bar>999</bar> <foo>14 </foo>xx <bar>998</bar> yyy<foo> 159</foo>z <bar>997</bar> ]] [[www<foo> 1 </foo> <bar>999</bar> <foo>2 </foo>xx <bar>998</bar> yyy<foo> 3</foo>z <bar>997</bar> ]]
      If you want to get rid of the leading/trailing whitespace, that's easily done. If you only have pre-5.10 regexes to work with, that can be handled too, although it's a bit more messy.


      Give a man a fish:  <%-{-{-{-<

        foo...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-05-20 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (73 votes). Check out past polls.

    Notices?