split function usage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • supriyamk
    New Member
    • Jul 2008
    • 12

    split function usage

    Hi,
    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 /\/, $_
    etc

    thanks in advance
  • bmerlover
    New Member
    • Jul 2007
    • 49

    #2
    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:

    Code:
    #you want to add 1 because you want to skip "\"
    $index_num = rindex($_, "\") + 1;
    
    #specific would be saved in $filename
    $filename = substr($_, $index_num); 
    
    #the directory would be saved into $directory path
    $dir_path = substr($_, 0, $index_num);

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      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.

      Regards,

      Jeff

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        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

        Comment

        • supriyamk
          New Member
          • Jul 2008
          • 12

          #5
          Thank you so much for the responses.

          Comment

          Working...