Connecting Tech Pros Worldwide Help | Site Map

Questions relating replacing a variable and file parse.

Member
 
Join Date: Mar 2007
Posts: 65
#1: Sep 24 '08
Hi,

I'm trying to replace a character in a string. was facing problem in it.

$string = "D:\Folder\TEST\abc.txt"

i wanted to transform this string to "D:/Folder/TEST/abc.txt"

this is wot i tried, but doesn't work

$string = ~ s/\\/\//;

Another Q is related to fileparse.

Expand|Select|Wrap|Line Numbers
  1. $value = D:/Folder/TEST/abc.txt
  2. my ( $name, $path, $extension ) = fileparse ( $value, '\..*' );
  3.     $filename = $name . $extension;
  4.  
I am getting the values as follows:

$name = abc
$path = /
$extension =


Where am I going wrong. Do i need to install anything thing for this. I have included File::Basename in the file.


Any help is appreciated.


Ravi
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Sep 24 '08

re: Questions relating replacing a variable and file parse.


Look at this:

Expand|Select|Wrap|Line Numbers
  1. $string = "D:\Folder\TEST\abc.txt";
  2. print $string;
the output is:

D:FolderTEST.bc.txt

notice there are no backslashes to substitute with forward slashes. The backslash in a double-quoted string is an escape character, not a literal backslash. You ned to use single-quotes:

Expand|Select|Wrap|Line Numbers
  1. $string = 'D:\Folder\TEST\abc.txt';

Are you trying to parse Windows filepaths on a Linux/Unix box?

From File::Basname:

<start quote>
fileparse_set_fstype

my $type = fileparse_set_fstype();
my $previous_type = fileparse_set_fstype($type);

Normally File::Basename will assume a file path type native to your current operating system (ie. /foo/bar style on Unix, \foo\bar on Windows, etc...). With this function you can override that assumption.

Valid $types are "MacOS", "VMS", "AmigaOS", "OS2", "RISCOS", "MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility), "Epoc" and "Unix" (all case-insensitive). If an unrecognized $type is given "Unix" will be assumed.
<end quote>
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,568
#3: Sep 24 '08

re: Questions relating replacing a variable and file parse.


Ravi,

The slash in the closing code tag goes the other way. It is a forward slash and not a back slash.

Please follow the example in the Reply Guidelines box as it shows the correct format.

Regards,

Jeff
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#4: Sep 25 '08

re: Questions relating replacing a variable and file parse.


just another note

when using Single quotes ' ' variables, arrays, etc. do not get printed/parsed eg.


Expand|Select|Wrap|Line Numbers
  1. $echo = "hello";
  2.  
  3. print ' $echo world';

this will print $echo world
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#5: Sep 25 '08

re: Questions relating replacing a variable and file parse.


Quote:

Originally Posted by Icecrack

just another note

when using Single quotes ' ' variables, arrays, etc. do not get printed/parsed eg.


Expand|Select|Wrap|Line Numbers
  1. $echo = "hello";
  2.  
  3. print ' $echo world';

this will print $echo world


True but thats not the case here. This is a lack of knowing the basics of perl beforing trying to write programs.
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#6: Sep 25 '08

re: Questions relating replacing a variable and file parse.


Quote:

Originally Posted by KevinADC

This is a lack of knowing the basics of perl beforing trying to write programs.

Are you talking about me again?

--Kevin
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#7: Sep 25 '08

re: Questions relating replacing a variable and file parse.


Quote:

Originally Posted by eWish

Are you talking about me again?

--Kevin

hehehe... were your ears buring?
Reply