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

replacing $` with ordinary capturing in global substitutions

by jbullock35 (Hermit)
on Sep 01, 2004 at 09:40 UTC ( #387458=perlquestion: print w/ replies, xml ) Need Help??
jbullock35 has asked for the wisdom of the Perl Monks concerning the following question:

$` can often be replaced by (.*?) at the head of a regular expression. But how can
$` be replaced if one wants to do a global substitution with /g? For example, how
can
s<(\w+)/><$`$1\n>g
be replaced by some variation that uses (.*?) or something close to it?

(To clarify, the substitution above replaces each match with a substring extending
from the beginning of the string through the last character of the match. So
Call/me/Ishmael/
becomes
Call
Call/me
Call/me/Ishmael
after the substitution.)

Thanks,
––John

Comment on replacing $` with ordinary capturing in global substitutions
Select or Download Code
Re: replacing $` with ordinary capturing in global substitutions
by borisz (Canon) on Sep 01, 2004 at 10:15 UTC
    s<(\w+)/><substr( $_, 0, pos $_). "$1\n">ge;
    Boris

      I guess you never tested this. pos() doesn't work inside of a s///:

      #!/usr/bin/perl -w use strict; $_= "This/is/a/test"; s<(\w+)/><$`$1\n>g; print $_,$/,$/; $_= "This/is/a/test"; s<(\w+)/><substr( $_, 0, pos $_). "$1\n">ge; print $_,$/;

      produces:

      This This/is This/is/a test Use of uninitialized value in substr at - line 9. Use of uninitialized value in substr at - line 9. Use of uninitialized value in substr at - line 9. This is a test

      - tye        

        Tested and it works fine for me.
        __OUTPUT__ This This/is This/is/a test This This/is This/is/a test ~ borisz$ perl -V Summary of my perl5 (revision 5 version 8 subversion 4) configuration: Platform: osname=darwin, osvers=7.3.0, archname=darwin-thread-multi-2level
        What perl version do you use?
        Boris
Re: replacing $` with ordinary capturing in global substitutions
by PodMaster (Abbot) on Sep 01, 2004 at 10:29 UTC
    one
    my @string = grep length, split '\/', 'Call/me/Ishmael/'; for my $i( 0 .. $#string ){ print join('/', @string[0..$i]),"\n"; } __END__ Call Call/me Call/me/Ishmael
    two
    local $fungus = ''; local $_ = 'Call/me/Ishmael/'; s' (\W*)? (\w+)? ' $fungus .= $1 if $1; $fungus .= $2 if $2; $2 ? "$fungus\n" : ""; 'gex; print __END__ Call Call/me Call/me/Ishmael
    update: nice one borisz++ (i thought about using pos for three, but figured nah)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: replacing $` with ordinary capturing in global substitutions
by Roy Johnson (Monsignor) on Sep 01, 2004 at 15:02 UTC
    my $old; s<\G(.*?\w+)><$old.=$1;"$old\n">ge;

    Caution: Contents may have been coded under pressure.
Re: replacing $` with ordinary capturing in global substitutions
by Anonymous Monk on Sep 02, 2004 at 09:30 UTC
    s<(\w+)/><@{[substr $_, 0, $-[0]]}$1\n>g;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://387458]
Approved by fireartist
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-12-29 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (187 votes), past polls