[32511] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3776 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 11 00:09:18 2012

Date: Mon, 10 Sep 2012 21:09:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 10 Sep 2012     Volume: 11 Number: 3776

Today's topics:
    Re: Setting backreference inside of a string <peter@makholm.net>
    Re: Setting backreference inside of a string <jwcarlton@gmail.com>
    Re: Setting backreference inside of a string <NoSpamPleaseButThisIsValid3@gmx.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 10 Sep 2012 09:59:22 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: Setting backreference inside of a string
Message-Id: <87mx0y1f6t.fsf@vps1.hacking.dk>

Jason C <jwcarlton@gmail.com> writes:

> $text = "Yes dear!"; $pattern = "(D|d)ear"; $replace = "$1eer";
>
> $text =~ s/$pattern/$replace/gi;

Using this code I get "Yes eer!" in $text...

> Any suggestions on how to make $1 in $replace refer to the first group
> in $pattern?

You need to look at the /e modifier to your substitution.

//Makholm


------------------------------

Date: Mon, 10 Sep 2012 02:36:22 -0700 (PDT)
From: Jason C <jwcarlton@gmail.com>
Subject: Re: Setting backreference inside of a string
Message-Id: <ecf2ae9a-a8bc-4748-a896-b52cd28b6c5d@googlegroups.com>

On Monday, September 10, 2012 3:57:47 AM UTC-4, Peter Makholm wrote:
> > $text = "Yes dear!"; $pattern = "(D|d)ear"; $replace = "$1eer";
> > $text =~ s/$pattern/$replace/gi;
> 
> Using this code I get "Yes eer!" in $text...

Could be a minor variation in what I posted vs. my actual code. I didn't post the whole thing because I thought it was unnecessarily complicated, but it's technically:

my $sth = $dbh->prepare("SELECT * FROM table");
  $sth->execute();

while (($pattern, $replace) = $sth->fetchrow_array()) {
  $text =~ s/(\b*)$pattern(er|in|ing|s|ed|y|\b)/$1$replace$+/gi;
}


> > Any suggestions on how to make $1 in $replace refer to the first group
> > in $pattern?
> 
> You need to look at the /e modifier to your substitution.

Thanks for the tip. I've read a bit on the 'e' modifier now, but I'm not quite understanding how to use it for this application.

In retrospect, what I think is happening is that the while() loop is treating $replace as if it is in a single quote instead of double. So instead of it reading like:

$pattern = "(D|d)ear";
$replace = "$1eer";

it's reading:

$pattern = '(D|d)ear';
$replace = '$1eer';

So the question may really be, how do I get it to read $replace as interpretive?


------------------------------

Date: Mon, 10 Sep 2012 13:10:02 +0200
From: Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net>
Subject: Re: Setting backreference inside of a string
Message-Id: <504dca8b$0$9525$9b4e6d93@newsspool1.arcor-online.net>

Am 10.09.2012 11:36, schrieb Jason C:
>>> Any suggestions on how to make $1 in $replace refer to the first group
>>> in $pattern?
>>
>> You need to look at the /e modifier to your substitution.
> 
> Thanks for the tip. I've read a bit on the 'e' modifier now, but I'm not quite understanding how to use it for this application.

For example like this:

$ perl -E '$r=q("${1}eer");($_="hello")=~s/(ll)/$r/ee; say'
helleero

- Wolf



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3776
***************************************


home help back first fref pref prev next nref lref last post