Re: substitution



Bowen, Bruce am Sonntag, 12. Februar 2006 22.31:
I have a text string = "^0176 ^0176"

I have set $a = "^0176 ^0176";
I have set $b = "^0176 ";

I'm using text =~ s/$a/$b/g;

And the text string doesn't change. I expected it to come out as "^0176 "
after the substitution. What is wrong with my logic?

Not shure if it's clear now after Tom's and Michael's posts.

Compared to the '\^'-solution, Tom's advice is generic (meaning that you have
not to know which meta characters - there are more than the caret - are in $a
and dont have to handle them explicitly). It's also more readable.

So it's the preferred way to do it.

perldoc perlre

explains all in full detail.

===
#!/usr/bin/perl
use strict; # don't forget
use warnings; # those two

my $a='^0176 ^0176'; # note the
my $b='^0176 '; # single quotes
my $text='some text ^0176 ^0176 here ^0176 ^0176 and there';

(my $temp=$text)=~s/\Q$a\E/$b/gs;
print "a) $temp\n";

my $a1=quotemeta($a);
(my $temp=$text)=~s/$a1/$b/gs;
print "b) $temp\n";
===

Hope $text does not contain votes ;-)

hth,
joe
.