944,132 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 13897
  • Perl RSS
Oct 9th, 2009
0

Perl: how to replace character '/' with '\'

Expand Post »
I am new to perl.
In a string How do I replace a backward slash with a forward slash?
when I call the following perl api $FindBin::Bin; I get the path as follows
$myPath = c:/perl/test
I would like to covert this to
c:\perl\test
I did try the following and get errors
#$myPath=~ tr/\//\/c;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alan_123 is offline Offline
3 posts
since Oct 2009
Oct 9th, 2009
0
Re: Perl: how to replace character '/' with '\'
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl -w
  2. #ReplaceBackslashWithSlash.pl
  3. use strict;
  4.  
  5. my $myPath = 'c:\perl\test'; # Example string containing backslashes
  6. print "\nOriginal string is: $myPath\n\n";
  7.  
  8. # The following command substitutes backslashes with forward slashes
  9. # You need to escape the backslash and slash with backslash
  10. $myPath =~ s/\\/\//g;
  11. print "Modified string is: $myPath\n";
Reputation Points: 153
Solved Threads: 142
Master Poster
d5e5 is offline Offline
740 posts
since Sep 2009
Oct 9th, 2009
0
Re: Perl: how to replace character '/' with '\'
Thanks for the replay,
Sorry I made a mistake. the source string is
'c:/perl/test'
would like to convert as
c:\perl\test
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alan_123 is offline Offline
3 posts
since Oct 2009
Oct 10th, 2009
0
Re: Perl: how to replace character '/' with '\'
Sorry, I read your example wrong. This should do it. Too bad the substitute command uses slashes to delimit the two strings. It confuses me, but it should work this time.
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl -w
  2. #ReplaceSlashWithBackslash.pl
  3. use strict;
  4.  
  5. my $myPath = 'c:/perl/test'; # Example string containing backslashes
  6. print "\nOriginal string is: $myPath\n\n";
  7.  
  8. # The following command replaces forward slashes with backslashes
  9. # You need to escape the backslash and slash with backslashes
  10. $myPath =~ s/\//\\/g; #There must be a better way, but I don't know it.
  11. print "Modified string is: $myPath\n";
Reputation Points: 153
Solved Threads: 142
Master Poster
d5e5 is offline Offline
740 posts
since Sep 2009
Oct 10th, 2009
0

about ur topic

Click to Expand / Collapse  Quote originally posted by d5e5 ...
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl -w
  2. #ReplaceBackslashWithSlash.pl
  3. use strict;
  4.  
  5. my $myPath = 'c:\perl\test'; # Example string containing backslashes
  6. print "\nOriginal string is: $myPath\n\n";
  7.  
  8. # The following command substitutes backslashes with forward slashes
  9. # You need to escape the backslash and slash with backslash
  10. $myPath =~ s/\\/\//g;
  11. print "Modified string is: $myPath\n";
why have u not write in 4th and 7th line
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rocky martin is offline Offline
1 posts
since Oct 2009
Oct 10th, 2009
0
Re: Perl: how to replace character '/' with '\'
Blank lines won't make any difference to how the program runs. Since I'm new to Perl I probably don't have a good sense of style yet. But I was trying to separate my code into three sections: introductory lines that appear in all programs, followed by setting up and printing the test string, followed by modifying and printing the resulting string.
Reputation Points: 153
Solved Threads: 142
Master Poster
d5e5 is offline Offline
740 posts
since Sep 2009
Oct 13th, 2009
0
Re: Perl: how to replace character '/' with '\'
Thank you all for your support
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alan_123 is offline Offline
3 posts
since Oct 2009
Oct 28th, 2009
0
Re: Perl: how to replace character '/' with '\'
no i have no any idea about this. i am thankful to you for this topic. i like it very much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maaria is offline Offline
2 posts
since Oct 2009
Dec 16th, 2011
0

Sed or Perl Help

Hey guys I'm stuck. I have been trying to write a sed or perl script to change the below. But each time I run the sed script or perl it completely zeros out the file.

I want to change this
/opt/dragon/bin/runit

to this
cd /opt/dragon/bin; /opt/dragon/bin/runit
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dozierc is offline Offline
2 posts
since Dec 2011
Dec 17th, 2011
0
Re: Perl: how to replace character '/' with '\'
Welcome to Daniweb. Please start a new thread to ask your question. When you ask your question in a new thread, it will help if you list the script that you tried. Then maybe someone can advise you why your script didn't give you the result you wanted.
Reputation Points: 153
Solved Threads: 142
Master Poster
d5e5 is offline Offline
740 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: split for multiple delimiters
Next Thread in Perl Forum Timeline: Sed or Perl Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC