Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: replacing text you have already replaced...

by wine (Scribe)
on Jul 24, 2001 at 22:13 UTC ( [id://99435]=note: print w/replies, xml ) Need Help??


in reply to replacing text you have already replaced...

I always use 1 while s///;, but you can also get that behavior with only using s///;:

my $data = "aaaaaaaaaaaaaaaaaaaaaaa"; # 23 a's $data =~ s/aa/pos() = print a/ge; print $data;

This outputs a in stead of aaaaaaaaaaaa

pos() normally returns the position where the regexpr matched the last time. Normally the s/// operator leaves of at that position. By using pos() as a lvalue you can reset this position. Therefore the s/// operator starts all over, mimmicking the 1 while s///g behavior.

I wouldn't use this under normal circumstantions though, since this is kind of a obfu. ;)

Replies are listed 'Best First'.
Re: Re: replacing text you have already replaced...
by suaveant (Parson) on Jul 24, 2001 at 22:24 UTC
    I wanted it to start from where it matched a token.. i.e.
    This is a template <TEMPLATE_DATA_FOOTER> mmm-kay
    and say <TEMPLATE_DATA_FOOTER> subs in <TEMPLATE_DATA_ADDRESS>... rather than restarting at the beginning I want to restart before <TEMPLATE_DATA_FOOTER>... I suppose I could set pos() = pos() - length($1) or something like that... that'd be weird...

                    - Ant

      I just had an idea for your problem, why not make your function recursive and do the repeated replacement there?

      hth
      -- Hofmator

Re: Re: replacing text you have already replaced...
by Hofmator (Curate) on Jul 25, 2001 at 13:51 UTC

    This does work as advertised but probably different than you thought. You should have used warnings ;-), your regex is equivalent to $data =~ s/aa//g; and that is not what you had in mind, is it? A small typo is the cause, you print a but I think wanted to print 'a', the letter 'a' and not nothing to the filehandle a.

    The important point is, pos() does not work with the substitution operator, only with m//g. See pos.

    So I guess we have to make do with the  1 while s/// solution.

    -- Hofmator

      I stand corrected, thank you for pointing it out. ;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-18 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found