Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > using $1 in a substitution replacement with variable interpolation

Reply
Thread Tools

using $1 in a substitution replacement with variable interpolation

 
 
imaginarywave@gmail.com
Guest
Posts: n/a
 
      05-01-2013
I am trying to use variable interpolation in a replacement string including $1, $2,...
However, I can't get it to expand $1 into the replacement. I eventually will have the
$pattern and $replacement variables be read from a configuration file, but even
setting them manually doesn't work.

In the example script, you can see that the $1 (which should be 'DEF') is not
expanded in $new_name, but it is in $new_name2.

Adding an 'e' flag to the substitution doesn't help.

How do I fix this?

Matt

EXAMPLE CODE:

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

my $old_name = 'ABC_DEF_GHI';

my $pattern = 'ABC_(...)_GHI';
my $replacement = 'CBA_${1}_IHG';

# using variables - doesn't work
my $new_name = $old_name;
$new_name =~ s|$pattern|$replacement|;

printf("%s --> %s\n", $old_name, $new_name);


# not using variables - does work
my $new_name2 = $old_name;
$new_name2 =~ s|ABC_(...)_GHI|CBA_${1}_IHG|;

printf("%s --> %s\n", $old_name, $new_name2);


OUTPUT:

ABC_DEF_GHI --> CBA_${1}_IHG
ABC_DEF_GHI --> CBA_DEF_IHG
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      05-01-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> my $pattern = 'ABC_(...)_GHI';
> my $replacement = 'CBA_${1}_IHG';
>
> # using variables - doesn't work
> my $new_name = $old_name;
> $new_name =~ s|$pattern|$replacement|;


It works to use s/$pattern/qq("$replacement")/ee.

First we do regular interpolation. With /e the replacement is trated as
perl code and therefore not interpolated:

-> s/ABC_(...)_GHI/qq("$replacement")/ee

Given the match we execute once:

-> s/ABC_(...)_GHI/"CBA_${1}_IHG"/e

And once more:

-> s/ABC_(...)_GHI/CBA_DEF_IHG/

Reaching the wanted result.

//Makholm
 
Reply With Quote
 
 
 
 
Charles DeRykus
Guest
Posts: n/a
 
      05-01-2013
On 5/1/2013 12:12 PM, (E-Mail Removed) wrote:
> I am trying to use variable interpolation in a replacement string including $1, $2,...
> However, I can't get it to expand $1 into the replacement. I eventually will have the
> $pattern and $replacement variables be read from a configuration file, but even
> setting them manually doesn't work.
>
> In the example script, you can see that the $1 (which should be 'DEF') is not
> expanded in $new_name, but it is in $new_name2.
>
> Adding an 'e' flag to the substitution doesn't help.
>
> ...
>
> #!/usr/local/bin/perl
> use strict;
>
> my $old_name = 'ABC_DEF_GHI';
>
> my $pattern = 'ABC_(...)_GHI';
> my $replacement = 'CBA_${1}_IHG';
>
> # using variables - doesn't work
> my $new_name = $old_name;
> $new_name =~ s|$pattern|$replacement|;
>
> printf("%s --> %s\n", $old_name, $new_name);
>


Perhaps, depending on input string complexity, you could just do this
and avoid a string eval:

(my $new_name=$old_name) = "${^PREMATCH}CBA_${1}_IHG${^POSTMATCH}"
if $old_name =~ m/ABC_(...)_GHI/p;



--
Charles DeRykus
 
Reply With Quote
 
Charles DeRykus
Guest
Posts: n/a
 
      05-01-2013
On 5/1/2013 4:30 PM, Charles DeRykus wrote:
>> ...
>>

>
> Perhaps, depending on input string complexity, you could just do this
> and avoid a string eval:
>
> (my $new_name=$old_name) = "${^PREMATCH}CBA_${1}_IHG${^POSTMATCH}"
> if $old_name =~ m/ABC_(...)_GHI/p;
>


Or, simply:

my $new_name = "${^PREMATCH}CBA_${1}_IHG${^POSTMATCH}"
if $old_name =~ m/ABC_(...)_GHI/p;

--
Charles DeRykus
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      05-02-2013
(E-Mail Removed) writes:

[...]

> #!/usr/local/bin/perl
> use strict;
>
> my $old_name = 'ABC_DEF_GHI';
>
> my $pattern = 'ABC_(...)_GHI';
> my $replacement = 'CBA_${1}_IHG';
>
> # using variables - doesn't work
> my $new_name = $old_name;
> $new_name =~ s|$pattern|$replacement|;
>
> printf("%s --> %s\n", $old_name, $new_name);


Using s/// in this way is a little weird. What you are really doing is
to apply a 'pattern' regex to some string in order to extract certain
parts out of it and then, you interpolate these parts into a template
string. I would use something like this for that:

-----------------
#!/usr/local/bin/perl
use strict;

sub interpolate
{
my ($in, $pat, $out) = @_;
my @matches;

@matches = $in =~ /$pat/;
$out =~ s/\$\{([0-9]+)\}/$matches[$1 - 1]/g;

return $out;
}


my $old_name = 'ABC_DEF_GHI';

my $pattern = 'ABC_(...)_(...)';
my $template = '${2}_${1}_IHG';

my $new_name = interpolate($old_name, $pattern, $template);


printf("%s --> %s\n", $old_name, $new_name);
 
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
name lookup failure using metaclasses with unittests Ulrich Eckhardt Python 7 04-12-2013 02:58 PM
USBLock : lock/unlock your computer with a USB key Sven Python 0 04-08-2013 03:21 PM
Regexp substitution with dynamic replacement string augukarl@yahoo.se Perl Misc 2 11-13-2007 01:44 PM
Variable Interpolation with %%variable phrankster Perl Misc 9 08-14-2005 12:19 AM
Over Interpolation of Variables in a Substitution Dietrich Perl Misc 3 07-25-2004 09:50 AM



Advertisments