467,114 Members | 1,289 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

Splitting a string!

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
  • viewed: 1216
Share:
2 Replies
eWish
Expert 512MB
Have a look at File::Basename.

--Kevin
Jul 17 '08 #2
KevinADC
Expert 2GB
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.

Similar topics

6 posts views Thread by qwweeeit | last post: by
5 posts views Thread by fatted | last post: by
2 posts views Thread by Trint Smith | last post: by
20 posts views Thread by Opettaja | last post: by
2 posts views Thread by CharChabil | last post: by
6 posts views Thread by HMS Surprise | last post: by
2 posts views Thread by shadow_ | last post: by
4 posts views Thread by yogi_bear_79 | last post: by
37 posts views Thread by xyz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.