Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: sequential substitutions

by anonymized user 468275 (Curate)
on Aug 03, 2018 at 10:08 UTC ( [id://1219789]=note: print w/replies, xml ) Need Help??


in reply to sequential substitutions

As stated it is simple enough, but there is usually more to the story, requiring more complex parsing than just detecting digits and substituting.
( echo '<foo>3</foo>'; echo '<foo>14</foo>'; echo '<foo>159</foo>') | +perl -e ' my $x = 0; while (my $l = <>) { $x++; $l =~ s/\d+/$x/; print $l; }' <foo>1</foo> <foo>2</foo> <foo>3</foo>

One world, one people

Replies are listed 'Best First'.
Re^2: sequential substitutions
by Anonymous Monk on Aug 03, 2018 at 10:48 UTC

    There is a little bit more to it (but not much), and I do apologize for over-simplifying.

    Let's suppose the code is actually:

    <foo>3</foo> <bar>a</bar> <foo>14</foo> <bar>bc</bar> <foo>159</foo> <bar>def</bar> [...]

    I want to simply echo lines that do not contain the "foo" tag to the output file.

      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>

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-29 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found