http://www.perlmonks.org?node_id=128730

Static has asked for the wisdom of the Perl Monks concerning the following question:

greetings monks --

trying to convert string '11/29/2001' to string '11\/29\/2001'.

i have attempted several conversions to no avail:

$dt =~ s/\//\\\//g $dt =~ s{/}{\/}g $dt =~ s{/}{\\/}g

all of my attempts thus far have failed to produce the desired result, all return either the original string or 11\\/29\\/2001.

i know this is an easy one, but it's trapped behind the cobwebs of many beers in my mind.

thanks for the help.

edited by footpad, ~Fri Nov 30 21:16:31 2001 (GMT)

Replies are listed 'Best First'.
Re: Escape from backslash hell
by Rich36 (Chaplain) on Dec 01, 2001 at 00:41 UTC
    Try using a different delimiter for the substitution other than "/". That tends to help cut down confusion when working with patterns that contain "/"'s.
    my $dt = '11/29/2001'; $dt =~ s|\/|\\/|g; print qq($dt);

    Rich36
    There's more than one way to screw it up...

      If you are going to avoid "leaning toothpicks" by using a different delimiter, then you might as well drop all of the useless ones:
      s:/:\\/:g;
      (Note that I don't like using metacharacters which are meaningful within a substitution. Hence my avoiding the pipes.)
Re: Escape from backslash hell
by patgas (Friar) on Dec 01, 2001 at 00:38 UTC

    Hmm, I'm not sure... I can't reproduce your problem...

    $_ = '11/29/2001'; # First I tried this... # s,/,\\/,g; s{/}{\\/}g; print;

    On my system, at least, both substitutions print 11\/29\/2001.

    "We're experiencing some Godzilla-related turbulence..."

Re: Escape from backslash hell
by Purdy (Hermit) on Dec 01, 2001 at 00:41 UTC
    I know several other Monks will jump on this, too, but here's my answer. :)

    #!/usr/bin/perl -w use strict; my $date = '11/29/2001'; print "DATE (before): $date\n"; $date =~ s/\//\\\//g; print "DATE (after): $date\n";

    Output:
    $ ./slash.pl
    DATE (before): 11/29/2001
    DATE (after): 11\/29\/2001

    Jason

Re: Escape from backslash hell
by lestrrat (Deacon) on Dec 01, 2001 at 00:39 UTC

    Eh, if you just want to convert the '/'s to '\/'s,

    my $str = '11/29/2001'; $str =~ s{/}{\\/}g; print $str, "\n";

    This seems to work for me...

Re: Escape from backslash hell
by Static (Initiate) on Dec 01, 2001 at 00:46 UTC
    this just in: i am a tool. i was using the debugger for this and after all attempts 'x $dt' was returning '11\\/29\\/2001', but if i print the variable it comes out golden. thx for the help.
Re: Escape from backslash hell
by dws (Chancellor) on Dec 01, 2001 at 07:24 UTC
    trying to convert string '11/29/2001' to string '11\/29\/2001'

    $converted = join '\/', split '/', '11/29/2001';

Re: Escape from backslash hell
by chipmunk (Parson) on Dec 01, 2001 at 09:57 UTC
    all of my attempts thus far have failed to produce the desired result, all return either the original string or 11\\/29\\/2001.
    I wonder if you're checking the results in the debugger... Be aware that if you examine the contents of a variable (with x, X, or V), the debugger will escape quotes and backslashes, in order to produce a valid quoted string. However, if you print the contents of a variable (with p or print), you get just the contents.

    Here's an example, showing that your second attempt works, although the debugger's output might be a bit misleading:

    DB<1> $_ = "2001/11/30" DB<2> s{/}{\\/}g DB<3> x $_ 0 '2001\\/11\\/30' DB<4> p $_ 2001\/11\/30 DB<5>
(crazyinsomniac) Re: Escape from backslash hell
by crazyinsomniac (Prior) on Dec 01, 2001 at 05:45 UTC
    Wow, at least 4 replies and all are off base. When you want to substitute characters with different ones you need to use tr aka y (see perlop).
    #untested but should work $dt =~ y{/}{\\/}; #or $dt =~ tr{/}{\\/};
    update: Bravo! You are soooo right! Glad to see some quality control here. I don't know why that "escaped" me, but hey, it is a valuable learning experience to realize that not all monks know what they are talking, and that you have to check out their references (like I should've done). Live and learn, and then go crazy.

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      I don't think this will work, since (AFAIK) tr can only translate a single character to another single character.

      Impossible Robot