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

split function usage

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

P: 50
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 2.5K+
P: 3,503
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 2.5K+
P: 4,059
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

P: 12
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.