Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > What is the RHS of a substitution?

Reply
Thread Tools

What is the RHS of a substitution?

 
 
Ben Bullock
Guest
Posts: n/a
 
      05-21-2006
This question is about substitutions like

s/XYZ/ABC/;

Is there a variable which contains the right hand part (ABC) of the most
recent substitution? I have a series of complex substitutions as part of a
while ( .. ) statement, and I would like to be able to use the right hand
part of the most recent match, but I'm not sure whether there is a simple
way to do this. Thanks for any help.

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-21-2006
Ben Bullock wrote:

> This question is about substitutions like
>
> s/XYZ/ABC/;
>
> Is there a variable which contains the right hand part (ABC) of the most
> recent substitution?


I don't believe so. It seems a rather unusual requirement.

>I have a series of complex substitutions as part of a
> while ( .. ) statement, and I would like to be able to use the right hand
> part of the most recent match, but I'm not sure whether there is a simple
> way to do this.


Using the /e qualifier you can put arbitrary Perl code in the RHS, for
example:

use strict;
use warnings;
$_='abc';

my $rhs;
while ( s/([[:lower:]])/$rhs=uc($1)/e ) {
print "$1 => $rhs\n";
}

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      05-21-2006
Ben Bullock schreef:

> This question is about substitutions like
>
> s/XYZ/ABC/;
>
> Is there a variable which contains the right hand part (ABC) of the
> most recent substitution? I have a series of complex substitutions as
> part of a while ( .. ) statement, and I would like to be able to use
> the right hand part of the most recent match, but I'm not sure
> whether there is a simple way to do this. Thanks for any help.


You could put the substitutions in an array, like this:

#!/usr/bin/perl
use strict ;
use warnings ;

use re 'eval' ; # check perlre!

use Data:umper ;

my @subst =
( ['ABC', '123'],
['DEF', 'GHI'],
['GHI', '789'],
['XYZ', 'ABC'],
) ;

my @done ;

while (<DATA>)
{
chomp ;

for my $i (0 .. $#subst)
{
my ($f, $t) = ( $subst[$i]->[0],
$subst[$i]->[1],
) ;

s~ $f (?{ push @done,
[
"line $.: $_",
"todo $i: s/$f/$t/",
]
})
~$t~xg
and push @{$done[$#done]}, "result: $_" ;
}
}

print Data:umper->Dump( [ \@subst, \@done ],
[qw(*subst *done)]
) ;

__DATA__
wqefewq XYZ skjd
gg g grewg gwg ewrgrewgrewg eagf
qfh GHI qfe ABC ewq DEF ddsf XYZ wfsqf
qckjqcd ABC qlkfjwqf

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-21-2006
Ben Bullock <(E-Mail Removed)> wrote:
> This question is about substitutions like
>
> s/XYZ/ABC/;
>
> Is there a variable which contains the right hand part (ABC) of the most
> recent substitution?



You can make one:

s/XYZ/ $last_match = 'ABC' /e;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-21-2006
Dr.Ruud schreef:

> my ($f, $t) = ( $subst[$i]->[0],
> $subst[$i]->[1],
> ) ;


By using Lexical::Alias, one can do

alias $subst[$i]->[0], my $f ;
alias $subst[$i]->[1], my $t ;

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-21-2006
Dr.Ruud schreef:

> my ($f, $t) = ( $subst[$i]->[0],
> $subst[$i]->[1],
> ) ;


Or use Data::Alias and change it to

my ($f, $t) = alias( $subst[$i]->[0],
$subst[$i]->[1],
) ;

Data::Alias seems to know a few more tricks than Lexical::Alias.

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      05-21-2006

Quoth "Ben Bullock" <(E-Mail Removed)>:
> This question is about substitutions like
>
> s/XYZ/ABC/;
>
> Is there a variable which contains the right hand part (ABC) of the most
> recent substitution? I have a series of complex substitutions as part of a
> while ( .. ) statement, and I would like to be able to use the right hand
> part of the most recent match, but I'm not sure whether there is a simple
> way to do this. Thanks for any help.


Err, just put it in a variable before you start?

my $foo = 'ABC';
s/XYZ/$foo/;
<more stuff in $foo>

Ben

--
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based
on the strictest morality. [Samuel Butler, paraphrased] (E-Mail Removed)
 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      05-22-2006
"Ben Morrow" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Quoth "Ben Bullock" <(E-Mail Removed)>:
>> This question is about substitutions like
>>
>> s/XYZ/ABC/;
>>
>> Is there a variable which contains the right hand part (ABC) of the most
>> recent substitution? I have a series of complex substitutions as part of
>> a
>> while ( .. ) statement, and I would like to be able to use the right hand
>> part of the most recent match, but I'm not sure whether there is a simple
>> way to do this. Thanks for any help.

>
> Err, just put it in a variable before you start?
>
> my $foo = 'ABC';
> s/XYZ/$foo/;
> <more stuff in $foo>


Thanks for your help, but this won't work. As I said, I have a series of
complex substitutions in a while statement. The above solution assumes that
the right hand side of the substitution is one fixed string in one
substitution. In fact each RHS involves all kinds of stuff involving $1, $2,
etc., and there is more than one substitution, because a series of
substitutions is being used as the truth test in a while statement. I want
to get the last matched RHS for the most recent round the loop. This is for
a semi-automated batch editing program.

However, the use of s/ /$var = /e does make sense, although I haven't tried
it in practice yet. Thanks to everyone who made a suggestion.

 
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
" *first = new string(*rhs.first);" cannot compile in MSVC fl C++ 10 01-12-2012 02:41 PM
Multiple RHS values in assignment? bbrady VHDL 2 01-27-2010 03:47 AM
Operator overloading - lhs, rhs? C++ 5 01-31-2006 10:32 PM
Operator overloading - lhs, rhs? C++ 1 01-31-2006 04:41 AM
How can I interpolate the RHS of a regex? Mr P Perl Misc 21 12-28-2005 04:37 PM



Advertisments