Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > s/// parametrized with backreferences

Reply
Thread Tools

s/// parametrized with backreferences

 
 
sln@netherlands.com
Guest
Posts: n/a
 
      03-14-2009
On Fri, 13 Mar 2009 17:42:34 -0500, Tad J McClellan <> wrote:

> <> wrote:
>> On Wed, 11 Mar 2009 16:24:17 -0700 (PDT), msciwoj <> wrote:

>
>>>The mystery for me is this 'second' evaluation actually, like you
>>>said, "interpolates the values for $2 and $1 into the replacement
>>>string" and it seems it's the only way that does the job.
>>>A bit of mystery for me but at the end it works and only this counts,
>>>I guess.

>
>
>> In the nut of it, evaling a variable with a value '$str'
>> will work,

>
>
>Because the value is a valid Perl expression.
>
>
>> but with a value of '$str\n$str' will not.

>
>
>Because the value is not a valid Perl expression.
>
>
>> But if you notice, the vaule of '$str\n$str' and '$str\\n$str'
>> are equal.

>
>
>That is how single quoted strings work.
>
>The "Scalar value constructors" section in perldata points out that
>there are only 2 special characters in a single quoted string.
>
>Single quote is special (it marks the end of the string) unless
>it is preceded by a backslash.
>
>Backslash is special if it preceeds a single quote or a backslash.
>
>A backslash that does not preceed 1 of those 2 characters is literal.
>
>So the 1st string above has a literal backslash and an "n" in it.
>
>The 2nd string above has a backslashed backslash (which becomes
>a backslash) and an "n" in it.
>
>
>> In escence, there is a conflict with the parser on some
>> level that is not quite understood

>
>
>Don't let that stop you from pretending to answer questions about it.
>
>
>> as pertaining to eval.

>
>
>It has nothing to do with eval.
>
>It has to do with single-quoted strings.
>
>
>> Whitness the below code. All works fine. Replace "replacement2" variable
>> with this value: '$2\n' and watch the error's.

>
>
>That is because in the code below, the value of $replacement2
>is a valid expression:
>
> perl -e '$2'
>
>But the value you suggest above is not a valid expression:
>
> perl -e '$2\n'
>


Oh, I get it. Its all about "valid expression" now and not eval in regex's.
And now since you have stated '$2' is a valid expression and '$2\n' isn't,
it raises the question of the ilogic in your nothing, so nothing argument.

Now, given the "valid expression" of '$2', this should be equivalent in terms of eval:

$replacement2 = '$2';
$data =~ /(.)\n(.)/;
$e = eval ($replacement2);
print "->",$e,"\n";

$data =~ s/(.)\n(.)/$replacement2/eg;
print $data."\n";

Unfortunately, you forgot your brain today Tad.

-sln

 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      03-14-2009
<> wrote:
> On Fri, 13 Mar 2009 17:42:34 -0500, Tad J McClellan <> wrote:
>


>>> In the nut of it, evaling a variable with a value '$str'
>>> will work,

>>
>>
>>Because the value is a valid Perl expression.
>>
>>
>>> but with a value of '$str\n$str' will not.

>>
>>
>>Because the value is not a valid Perl expression.



> Oh, I get it. Its all about "valid expression"



You're welcome.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      03-14-2009
On Fri, 13 Mar 2009 21:48:55 -0500, Tad J McClellan <> wrote:

> <> wrote:
>> On Fri, 13 Mar 2009 17:42:34 -0500, Tad J McClellan <> wrote:
>>

>
>>>> In the nut of it, evaling a variable with a value '$str'
>>>> will work,
>>>
>>>
>>>Because the value is a valid Perl expression.
>>>
>>>
>>>> but with a value of '$str\n$str' will not.
>>>
>>>
>>>Because the value is not a valid Perl expression.

>
>
>> Oh, I get it. Its all about "valid expression"

>
>
>You're welcome.


No thanks! But you are welcome.

Here's some valid expression for ya Tad:

$replacement2 = '$2';
$data =~ /(.)\n(.)/;
$e = eval ($replacement2);
print "->",$e,"\n";

$data =~ s/(.)\n(.)/$replacement2/eg;
print $data."\n";


 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-14-2009
<> wrote:
> On Fri, 13 Mar 2009 21:48:55 -0500, Tad J McClellan <> wrote:
>
>> <> wrote:
>>> On Fri, 13 Mar 2009 17:42:34 -0500, Tad J McClellan <> wrote:
>>>

>>
>>>>> In the nut of it, evaling a variable with a value '$str'
>>>>> will work,
>>>>
>>>>
>>>>Because the value is a valid Perl expression.
>>>>
>>>>
>>>>> but with a value of '$str\n$str' will not.
>>>>
>>>>
>>>>Because the value is not a valid Perl expression.

>>
>>
>>> Oh, I get it. Its all about "valid expression"

>>
>>
>>You're welcome.

>
> No thanks! But you are welcome.
>
> Here's some valid expression for ya Tad:
>
> $replacement2 = '$2';
> $data =~ /(.)\n(.)/;
> $e = eval ($replacement2);
> print "->",$e,"\n";
>
> $data =~ s/(.)\n(.)/$replacement2/eg;
> print $data."\n";



That works as expected. Since there is only one eval with s///e,
the result after evaluating $replacement2 is the string '$2'.

What were you expecting it to do instead?

If you were expecting the last print to print the value of the $2
variable, then you don't have enough levels of eval to get to where
'$2' is evaluated.

The 2nd level of eval can be gotten with either

$data =~ s/(.)\n(.)/$replacement2/eeg;

or

$data =~ s/(.)\n(.)/$e/eg;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Backreferences in python ? Pankaj Python 7 01-24-2006 09:15 AM
backreferences Amy Dillavou Python 4 09-28-2005 09:03 PM
regular expressions - math on backreferences Chris Nolte Perl 9 05-25-2004 07:43 PM
How to use backreferences in a variable for a regular expression Mark Fletcher Perl 1 05-19-2004 11:12 AM
java.util.regex: Backreferences? dhek bhun kho Java 2 07-09-2003 11:29 AM



Advertisments