Brad.Walker has asked for the wisdom of the Perl Monks concerning the following question:

I can't for the life of me figure out how to change the following string from
\\10.0.0.1\path\to\foo
to
//10.0.0.1/path/to/foo

I know to use the "tr" operator. But, I can't figure out the syntax.

Many thanks as this is driving me crazy.

Replies are listed 'Best First'.
Re: transliteration of a string
by ikegami (Patriarch) on Sep 14, 2007 at 21:32 UTC
    my $s = '\\\\10.0.0.1\\path\\to\\foo'; print("$s\n"); # \\10.0.0.1\path\to\foo $s =~ tr/\\/\//; print("$s\n"); # //10.0.0.1/path/to/foo

    You can use an alternate delimiter to make things a bit more readable.

    my $s = '\\\\10.0.0.1\\path\\to\\foo'; print("$s\n"); # \\10.0.0.1\path\to\foo $s =~ tr{\\}{/}; print("$s\n"); # //10.0.0.1/path/to/foo
Re: transliteration of a string
by mjscott2702 (Pilgrim) on Sep 14, 2007 at 23:58 UTC
    You can also use the hex code for the characters in question, sometimes useful (and less noisy) than escaping everything (or use alternate delimiters as ikegami suggested). Also useful is you want to use some characters that may not be easily entered, such as converting between the Euro currency symbol and the US dollar symbol:
    while(<DATA>) { chomp; print "$_\t"; tr /\x5c/\x2f/; print "$_\n"; } __DATA__ \\10.0.0.1\path\to\foo
Re: transliteration of a string
by perlofwisdom (Pilgrim) on Sep 15, 2007 at 04:18 UTC
    You can also try using regular expression substitution:
    my $slashes = '\\\\10.0.0.1\path\to\foo'; print "before: $slashes\n"; $slashes =~ s/\\/\//g; print "after.: $slashes\n";
    Which yields...
    before: \\10.0.0.1\path\to\foo after.: //10.0.0.1/path/to/foo