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

Splitting a string!

P: 68
Hi,


How to split this string:
I wanted the filename and directory path in two different variables.
I tried this, but it doesn't work


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. my($filename, $directories, $suffix) = fileparse("C:\WM603\PUBLIC\stdlib.h");
  4.  
  5. print "<br>Filename  : " . $filename    . "\n";
  6. print "<br>Directory : " . $directories . "\n<br><br><br>";
  7.  
  8.  

My output is:

Filename : C:WM603PUBLICstdlib.h
Directory : ./

Any help is appreciated


Thanks,


Ravi
Jul 17 '08 #1
Share this Question
Share on Google+
2 Replies


eWish
Expert 100+
P: 971
Have a look at File::Basename.

--Kevin
Jul 17 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Hi,


How to split this string:
I wanted the filename and directory path in two different variables.
I tried this, but it doesn't work


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. my($filename, $directories, $suffix) = fileparse("C:\WM603\PUBLIC\stdlib.h");
  4.  
  5. print "<br>Filename  : " . $filename    . "\n";
  6. print "<br>Directory : " . $directories . "\n<br><br><br>";
  7.  
  8.  

My output is:

Filename : C:WM603PUBLICstdlib.h
Directory : ./

Any help is appreciated


Thanks,


Ravi

You did not read the File::Basename documentation very well. Plus backslashes inside a double-quoted string have the danger of being interpolated as meta characters, although I don't think \W or \P or \s are a problem. Still, you should use forward slashes even when using Windows in your directory paths and use single-quotes unless there is a varaible or meta character inside the string that needs to be interpolated and expanded.

Expand|Select|Wrap|Line Numbers
  1.  
  2. use File::Basename;
  3. my($filename, $directories, $suffix) = fileparse('C:/WM603/PUBLIC/stdlib.h', qr/\.(.*)$/);
  4.  
  5. print "<br>Filename  :  $filename\n";
  6. print "<br>Directory : $directories\n";
  7. print "<br>Suffix : $suffix\n"; 
  8.  
  9.  
Jul 18 '08 #3

Post your reply

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