Splitting a string!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thanawala27
    New Member
    • Mar 2007
    • 68

    #1

    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


    Code:
    
    my($filename, $directories, $suffix) = fileparse("C:\WM603\PUBLIC\stdlib.h");
    
    print "<br>Filename  : " . $filename    . "\n";
    print "<br>Directory : " . $directories . "\n<br><br><br>";

    My output is:

    Filename : C:WM603PUBLICst dlib.h
    Directory : ./

    Any help is appreciated


    Thanks,


    Ravi
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Have a look at File::Basename.

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by thanawala27
      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


      Code:
      
      my($filename, $directories, $suffix) = fileparse("C:\WM603\PUBLIC\stdlib.h");
      
      print "<br>Filename  : " . $filename    . "\n";
      print "<br>Directory : " . $directories . "\n<br><br><br>";

      My output is:

      Filename : C:WM603PUBLICst dlib.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.

      Code:
      use File::Basename;
      my($filename, $directories, $suffix) = fileparse('C:/WM603/PUBLIC/stdlib.h', qr/\.(.*)$/);
      
      print "<br>Filename  :  $filename\n";
      print "<br>Directory : $directories\n";
      print "<br>Suffix : $suffix\n";

      Comment

      Working...