468,321 Members | 1,679 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Questions relating replacing a variable and file parse.

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
Sep 24 '08 #1
6 2492
KevinADC
4,059 Expert 2GB
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>
Sep 24 '08 #2
numberwhun
3,503 Expert Mod 2GB
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
Sep 24 '08 #3
Icecrack
174 Expert 100+
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
Sep 24 '08 #4
KevinADC
4,059 Expert 2GB
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.
Sep 25 '08 #5
eWish
971 Expert 512MB
This is a lack of knowing the basics of perl beforing trying to write programs.
Are you talking about me again?

--Kevin
Sep 25 '08 #6
KevinADC
4,059 Expert 2GB
Are you talking about me again?

--Kevin
hehehe... were your ears buring?
Sep 25 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by xtian | last post: by
8 posts views Thread by jamesfin | last post: by
7 posts views Thread by Rob Meade | last post: by
6 posts views Thread by tentstitcher | last post: by
14 posts views Thread by shamirza | last post: by
6 posts views Thread by Dic4000 | last post: by
6 posts views Thread by saif.shakeel | last post: by
7 posts views Thread by saif.shakeel | last post: by
6 posts views Thread by Bartholomew Simpson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.