Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

change forward slash to backslash

by Eric23 (Initiate)
on Nov 20, 2005 at 06:40 UTC ( #510208=perlquestion: print w/ replies, xml ) Need Help??
Eric23 has asked for the wisdom of the Perl Monks concerning the following question:

Hi every1: I am a newer, i have a very simple question. How can i transform c:/windows/ to c:\windows\ Thank you very much

2005-11-21 Retitled by g0n, as per Monastery guidelines
Original title: 'Very simple question~!'

Comment on change forward slash to backslash
Re: change forward slash to backslash
by davido (Archbishop) on Nov 20, 2005 at 06:52 UTC

    Here are a couple of ways:

    First, transliteration:

    $string =~ tr{/}{\};

    Or with the substitution operator:

    $string = s{/}{\}g;

    tr/// is going to be a little faster, but s/// would let you create more complex matching and replacing criteria.

    For further reading, see perlop. There you'll find an explanation of the s/// and tr/// operators. Also see perlre for a more detailed discussion of what you can do with the s/// operator.

    Update:Oops... yes, those pesky backslash characters need to be doubled up thanks to the gory details of quote parsing. tr{/}{\\}, for example. Sorry about that. ;)


    Dave

Re: change forward slash to backslash
by tirwhan (Abbot) on Nov 20, 2005 at 06:53 UTC

    See this thread.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      Thank you for answer. But it seem doesn't work. $string = 'c:/windows/'; $string =~ tr{/}{\}; print $string; Am I right?? But when I run I get this message "Transliteration replacement not terminated at AIFF_test.pl" why?

        The \ needs to be quoted. It is an 'escape' character that allows you to treat special characters normally and normal characters specially. In this case it is quoting the }, which is not what you want.

        use warnings; use strict; my $str = 'c:/windows/'; $str =~ tr{/}{\\}; print $str;

        Prints:

        c:\windows\

        DWIM is Perl's answer to Gödel
Re: change forward slash to backslash
by vek (Prior) on Nov 20, 2005 at 15:44 UTC

    If you plan on using that directory as part of a filename, you could always give File::Spec a try. Not only will it do what you want by converting C:/windows to C:\windows, it helps you write portable code.

    use File::Spec; use strict; use warnings; my $dir = 'c:/temp'; my $file = 'tempfile'; my $path = File::Spec->catfile($dir, $file); print $path, "\n";
    Which will output C:\temp\tempfile

    -- vek --

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://510208]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2014-09-22 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls