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

S

sv2021

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;
 
G

Gunnar Hjalmarsson

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';
 
G

Gunnar Hjalmarsson

Asim said:
Replace the \ by \\ in the substitution.

Or even better: use the more suitable and more efficient tr/// operator:

$test =~ tr|\\|/|;
 
J

Jürgen Exner

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top