Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
The stupid question is the question not asked
 
PerlMonks  

Escape from backslash hell

by Static (Initiate)
on Nov 30, 2001 at 19:31 UTC ( [id://128730]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 patgas (Friar) on Nov 30, 2001 at 19: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 lestrrat (Deacon) on Nov 30, 2001 at 19: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 Purdy (Hermit) on Nov 30, 2001 at 19: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 Rich36 (Chaplain) on Nov 30, 2001 at 19: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 Static (Initiate) on Nov 30, 2001 at 19: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.
(crazyinsomniac) Re: Escape from backslash hell
by crazyinsomniac (Prior) on Dec 01, 2001 at 00: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
Re: Escape from backslash hell
by dws (Chancellor) on Dec 01, 2001 at 02: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 04: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>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128730]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.