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

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 3
Reputation: alan_123 is an unknown quantity at this point 
Solved Threads: 0
alan_123 alan_123 is offline Offline
Newbie Poster

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

 
0
  #1
Oct 9th, 2009
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;
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster
 
0
  #2
Oct 9th, 2009
  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";
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: alan_123 is an unknown quantity at this point 
Solved Threads: 0
alan_123 alan_123 is offline Offline
Newbie Poster
 
0
  #3
Oct 9th, 2009
Thanks for the replay,
Sorry I made a mistake. the source string is
'c:/perl/test'
would like to convert as
c:\perl\test
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster
 
0
  #4
Oct 10th, 2009
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.
  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";
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: rocky martin is an unknown quantity at this point 
Solved Threads: 1
rocky martin rocky martin is offline Offline
Newbie Poster

about ur topic

 
0
  #5
Oct 10th, 2009
Originally Posted by d5e5 View Post
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster
 
0
  #6
Oct 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: alan_123 is an unknown quantity at this point 
Solved Threads: 0
alan_123 alan_123 is offline Offline
Newbie Poster
 
0
  #7
Oct 13th, 2009
Thank you all for your support
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: maaria is an unknown quantity at this point 
Solved Threads: 0
maaria maaria is offline Offline
Newbie Poster
 
0
  #8
Oct 28th, 2009
no i have no any idea about this. i am thankful to you for this topic. i like it very much.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC