How to replace the "\" in "C:\myrootr\folder" with "/" ?

Discussion in 'Perl Misc' started by sv2021@gmail.com, Jun 26, 2007.

  1. Guest

    Hi,
    Can someone help me. I know this should be easy but I coulnt get it
    working.

    I want to replace the "\" backslash character in a string e.g: my
    $test = "C:\myroot\folder"; with forward slash "/"

    This is what my snippet, which seems to be not doing anything....
    $test =~ s|\|/|g;
    , Jun 26, 2007
    #1
    1. Advertising

  2. wrote:
    > I want to replace the "\" backslash character in a string e.g: my
    > $test = "C:\myroot\folder"; with forward slash "/"


    That needs to be written

    my $test = "C:\\myroot\\folder";

    or

    my $test = 'C:\myroot\folder';

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 26, 2007
    #2
    1. Advertising

  3. Asim Suter wrote:
    > <> wrote:
    >> I want to replace the "\" backslash character in a string e.g: my
    >> $test = "C:\myroot\folder"; with forward slash "/"
    >>
    >> This is what my snippet, which seems to be not doing anything....
    >> $test =~ s|\|/|g;

    >
    > Replace the \ by \\ in the substitution.


    Or even better: use the more suitable and more efficient tr/// operator:

    $test =~ tr|\\|/|;

    > As \ has special special meaning - you need to "escape" it.


    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 26, 2007
    #3
  4. wrote:
    > I want to replace the "\" backslash character in a string e.g: my
    > $test = "C:\myroot\folder"; with forward slash "/"


    Do you really have a file that is named
    c:myroot[formfeed]older
    where [formfeed] indicates the form feed character?
    Or did you mean
    $test = 'C:\myroot\folder';
    If so, then please be advised that using backward slashes in filenames is
    not a bad idea (you just saw one reason why) and you should use forward
    slashes instead, even on Windows which will happily work with forward
    slashes, too.

    > This is what my snippet, which seems to be not doing anything....
    > $test =~ s|\|/|g;


    Really? For me it is generating two error messages:
    Unrecognized escape \m passed through at [...]
    Substitution replacement not terminated at [...]
    You didn't get those?

    Your substitute command will replace globally a vertical bar followed by a
    forward slash against .... well, you didn't write the substitution string.

    Did you mean
    $test =~ s|\\|/|g;
    instead?
    Anyway, for this kind of transliteration the tr() command is more suitable:
    $test =~ tr|\\|/|;

    jue
    Jürgen Exner, Jun 26, 2007
    #4
    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. Brian Blais
    Replies:
    1
    Views:
    373
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    342
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,495
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    226
    Dr John Stockton
    Aug 27, 2004
  5. Replies:
    3
    Views:
    172
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page