Re: substitution
- From: security.department@xxxxxxxx (Hans Meier)
- Date: Mon, 13 Feb 2006 17:31:01 +0100
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
.
- References:
- substitution
- From: Bruce Bowen
- substitution
- Prev by Date: Re: substitution
- Next by Date: Re: single step - debug mode
- Previous by thread: Re: substitution
- Next by thread: RE: substitution
- Index(es):
Relevant Pages
|