467,179 Members | 1,293 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

split function usage

in order to split a path i used split m!/!, $Dir_name.
when i print the path, it gives \, so i tried different combinations but none are working,
please let me know.
i tried -
split m!\!, $_
split /\/, $_

thanks in advance
Jul 7 '08 #1
  • viewed: 983
4 Replies
I'm not sure if you specifically need the split function or not. I usually use the index() and rindex() function to seperate the directory paths from each other. For example: if you want to split c:\program files\software\specific. And you wanted to split "specific" from the the rest of the directory path. I would do the following:

Expand|Select|Wrap|Line Numbers
  2. #you want to add 1 because you want to skip "\"
  3. $index_num = rindex($_, "\") + 1;
  5. #specific would be saved in $filename
  6. $filename = substr($_, $index_num); 
  8. #the directory would be saved into $directory path
  9. $dir_path = substr($_, 0, $index_num);
Jul 8 '08 #2
Expert Mod 2GB
To split up a path, the best thing to use is the File::Basename module. It is a much more efficient way to accomplish this task.


Jul 8 '08 #3
Expert 2GB
The backslash needs to be escaped, you need two of them, the first escapes the second one:

split /\\/, $_

But I also reccomend you use File::Basename
Jul 8 '08 #4
Thank you so much for the responses.
Jul 8 '08 #5

Post your reply

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

Similar topics

7 posts views Thread by brianshields@gmail.com | last post: by
12 posts views Thread by Martin Dieringer | last post: by
2 posts views Thread by SL_McManus | last post: by
15 posts views Thread by zealotcat@gmail.com | last post: by
1 post views Thread by mdefoor | last post: by
1 post views Thread by John | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.