By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Questions relating replacing a variable and file parse.

P: 68
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
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
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
Expert Mod 2.5K+
P: 3,503
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
Expert 100+
P: 174
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
Expert 2.5K+
P: 4,059
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
Expert 100+
P: 971
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
Expert 2.5K+
P: 4,059
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.