Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Struggle with "simple" replacement

Reply
Thread Tools

Struggle with "simple" replacement

 
 
Arne
Guest
Posts: n/a
 
      06-15-2004
Trying to make a simple replacement of strings I spent some hours
resulting in this:

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

$_ = "Just {Test4*4}";
#my $old = "{Test4*4}"; #(i) normal double quoted match
my $old = "\{Test4\*4\}"; #(ii) escaping meta chars
#my $old = '{Test4*4}'; #(iii) using single quotes
my $new = "okay";
s/$old/$new/;
print "$_\n";

It should just demonstrate how to substitute a substring containing
meta chars (e.g. { or *). I had expected that avoiding interpolation
by escaping would help. But even single quote strings aren't better.
Tell me why I am such a fool. would appreciate.

Arne
 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      06-15-2004
On 14 Jun 2004 21:59:11 -0700, Arne <(E-Mail Removed)> wrote:
> Trying to make a simple replacement of strings I spent some hours
> resulting in this:
>
> #!/usr/bin/perl -w
> #use strict;
>
> $_ = "Just {Test4*4}";
> #my $old = "{Test4*4}"; #(i) normal double quoted match
> my $old = "\{Test4\*4\}"; #(ii) escaping meta chars


That doesn't escape anything. { and * are not "special" in
a double quoted string so the backslashes are essentially ignored
(or \{ is converted to { and so on).

> #my $old = '{Test4*4}'; #(iii) using single quotes
> my $new = "okay";
> s/$old/$new/;
> print "$_\n";
>
> It should just demonstrate how to substitute a substring containing
> meta chars (e.g. { or *). I had expected that avoiding interpolation
> by escaping would help. But even single quote strings aren't better.
> Tell me why I am such a fool. would appreciate.


perldoc -f quotemeta

or to do things the hard way:

"\\{Test4\\*4\\}"

or

'\{Test4\*4\}'

--
Sam Holden
 
Reply With Quote
 
 
 
 
Arne
Guest
Posts: n/a
 
      06-15-2004
Truth is simple:

> perldoc -f quotemeta


is the golden hint. Thank u so much Sam. (was my first posting so that
I cannot back off...)

Arne
 
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
A question for all those who struggle with automatically databoundcontrols Phil H ASP .Net Web Controls 1 12-21-2007 05:44 AM
Undo Operation Struggle jay Java 6 02-25-2006 06:39 PM
Struggle with The Move to .NET =?Utf-8?B?TWljaGFlbCBHZWlzdA==?= ASP .Net 21 11-24-2004 09:24 PM
Replacement For Access Point KS Wireless Networking 1 06-24-2004 04:40 AM
Re: Do I need to struggle to barely survive? Thund3rstruck Computer Support 0 11-11-2003 04:38 PM



Advertisments