Questions relating replacing a variable and file parse.

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

    Questions relating replacing a variable and file parse.

    Hi,

    I'm trying to replace a character in a string. was facing problem in it.

    $string = "D:\Folder\TEST \abc.txt"

    i wanted to transform this string to "D:/Folder/TEST/abc.txt"

    this is wot i tried, but doesn't work

    $string = ~ s/\\/\//;

    Another Q is related to fileparse.

    Code:
    $value = D:/Folder/TEST/abc.txt
    my ( $name, $path, $extension ) = fileparse ( $value, '\..*' );
    	$filename = $name . $extension;
    I am getting the values as follows:

    $name = abc
    $path = /
    $extension =


    Where am I going wrong. Do i need to install anything thing for this. I have included File::Basename in the file.


    Any help is appreciated.


    Ravi
    Last edited by numberwhun; Sep 24 '08, 10:52 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Look at this:

    Code:
    $string = "D:\Folder\TEST\abc.txt";
    print $string;
    the output is:

    D:FolderTEST.bc .txt

    notice there are no backslashes to substitute with forward slashes. The backslash in a double-quoted string is an escape character, not a literal backslash. You ned to use single-quotes:

    Code:
    $string = 'D:\Folder\TEST\abc.txt';

    Are you trying to parse Windows filepaths on a Linux/Unix box?

    From File::Basname:

    <start quote>
    fileparse_set_f stype

    my $type = fileparse_set_f stype();
    my $previous_type = fileparse_set_f stype($type);

    Normally File::Basename will assume a file path type native to your current operating system (ie. /foo/bar style on Unix, \foo\bar on Windows, etc...). With this function you can override that assumption.

    Valid $types are "MacOS", "VMS", "AmigaOS", "OS2", "RISCOS", "MSWin32", "DOS" (also "MSDOS" for backwards bug compatibility), "Epoc" and "Unix" (all case-insensitive). If an unrecognized $type is given "Unix" will be assumed.
    <end quote>

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Ravi,

      The slash in the closing code tag goes the other way. It is a forward slash and not a back slash.

      Please follow the example in the Reply Guidelines box as it shows the correct format.

      Regards,

      Jeff

      Comment

      • Icecrack
        Recognized Expert New Member
        • Sep 2008
        • 174

        #4
        just another note

        when using Single quotes ' ' variables, arrays, etc. do not get printed/parsed eg.


        Code:
        $echo = "hello";
        
        print ' $echo world';

        this will print $echo world

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by Icecrack
          just another note

          when using Single quotes ' ' variables, arrays, etc. do not get printed/parsed eg.


          Code:
          $echo = "hello";
          
          print ' $echo world';

          this will print $echo world

          True but thats not the case here. This is a lack of knowing the basics of perl beforing trying to write programs.

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            Originally posted by KevinADC
            This is a lack of knowing the basics of perl beforing trying to write programs.
            Are you talking about me again?

            --Kevin

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by eWish
              Are you talking about me again?

              --Kevin
              hehehe... were your ears buring?

              Comment

              Working...