Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to replace the "\" in "C:\myrootr\folder" with "/" ?

Reply
Thread Tools

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

 
 
sv2021@gmail.com
Guest
Posts: n/a
 
      06-26-2007
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;

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-26-2007
Asim Suter wrote:
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-26-2007
(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
pyrex functions to replace a method (Re: replace a method in class:how?) Brian Blais Python 1 06-27-2006 12:13 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments