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.

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 ;-)


Relevant Pages

  • CfV: Text Substitution
    ... Text substitution is useful for a number of applications, ... as a string in the active currency. ... chosen as the default delimiter because it is an illegal character ... Set the string defined by c-addr1 and len1 as the text to substitute ...
  • Re: DCL question (of the day)- null byte in string symbol
    ... I have pointed out that this code was just to show the bug, ... Subject: Re: DCL question - null byte in string symbol ... Lexical substitution is a possibility. ...
  • Re: performance surprise -- why?
    ... On 25 Aug 2004, Anno Siegel wrote: ... >> string to a file and counted the lengths of the lines using a simple ... The substitution method must move parts of the ... > The results show indexing and global matching in the same ballpark, ...
  • Re: Flames over -- Re: Which is simpler?
    ... Am Sonntag, 19. ... Februar 2006 22:02 schrieb Andrew Morton: ... But how do you handle memory management? ...
  • Re: DCL question (of the day)- null byte in string symbol
    ... The string isn't that long. ... Note that is 7 bits of the second character, 8 bits of ... Lexical substitution is a possibility. ... The fact that command verification reflects a truncated command line ...