Substitution Problem

Discussion in 'Perl' started by Ashok, Jul 18, 2004.

  1. Ashok

    Ashok Guest

    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.
    Ashok, Jul 18, 2004
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Jul 18, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krispen Hartung
    Replies:
    1
    Views:
    673
    Hywel Jenkins
    Dec 11, 2004
  2. ian mayo
    Replies:
    0
    Views:
    331
    ian mayo
    Sep 23, 2003
  3. Florian Schulze

    problem with newlines in regexp substitution

    Florian Schulze, Feb 23, 2006, in forum: Python
    Replies:
    1
    Views:
    269
    James Stroud
    Feb 23, 2006
  4. Ed Keith

    Raw string substitution problem

    Ed Keith, Dec 16, 2009, in forum: Python
    Replies:
    1
    Views:
    249
    Chris Hulan
    Dec 16, 2009
  5. Gabriel Genellina

    Re: Raw string substitution problem

    Gabriel Genellina, Dec 16, 2009, in forum: Python
    Replies:
    13
    Views:
    654
Loading...

Share This Page