Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Substitution Problem

Reply
Thread Tools

Substitution Problem

 
 
Ashok
Guest
Posts: n/a
 
      07-18-2004
I am having some problem with the substitution operation. This is what
I am trying to do:

Get a directory path from the user and also the search string in the
path and remove the string from the path.

Input String: c:\ashok\temp\perl
Search String: c:\ashok

So the output should be temp\perl.

The Program I wrote looks like this:

$input = <STDIN>;
$search = <STDIN>;

$input =~ s/$search//;
print $input;

But this does not work. It looks like there is some confusion with the
special characters :, \ .
Can somebody tell me what's wrong and how do I fix this?

Thanks,
Ashok.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-18-2004
Ashok wrote:
> I am having some problem with the substitution operation. This is what
> I am trying to do:
>
> Get a directory path from the user and also the search string in the
> path and remove the string from the path.
>
> Input String: c:\ashok\temp\perl
> Search String: c:\ashok
>
> So the output should be temp\perl.


I suppose you mean \temp\perl - or did I miss a step?

> The Program I wrote looks like this:
>
> $input = <STDIN>;
> $search = <STDIN>;
>
> $input =~ s/$search//;
> print $input;
>
> But this does not work. It looks like there is some confusion with the
> special characters :, \ .


Do not just guess which characters are special in a Perl pattern. Look
it up in "perldoc perlre". There you can also read about how to quote
pattern metacharacters when needed.

> Can somebody tell me what's wrong and how do I fix this?


Besides the unquoted backslash, $search includes an EOL character.
Best fixed with the chomp() function.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
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
Re: Raw string substitution problem Gabriel Genellina Python 13 01-02-2010 06:56 AM
Raw string substitution problem Ed Keith Python 1 12-16-2009 02:36 PM
problem with newlines in regexp substitution Florian Schulze Python 1 02-23-2006 09:10 PM
Problem with email form script and %7E tilde (~) substitution Krispen Hartung HTML 1 12-11-2004 10:46 AM
XML substitution problem (simulation) ian mayo XML 0 09-23-2003 11:04 AM



Advertisments