Substitution Problem

A

Ashok

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.
 
G

Gunnar Hjalmarsson

Ashok said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top