Help me get "Directory" out of full windows path name?

Discussion in 'Perl Misc' started by Random Task, Feb 21, 2006.

  1. Random Task

    Random Task Guest

    Hi i am trying to get $tmpdir to contain the "directory" instead of a
    fully pathed file name ... i.e. truncate the file name.

    I am trying the below code ... but $1 is always empty ... help please :)

    my $tmpdir = $file;
    if ( $tmpdir =~/([.]*)[\\]{1}/)
    {
    print("MATCH\n");
    $tmpdir = $1;
    print ("[--->]".$1."<----]\n");
    }
    else
    {
    print("NO MATCH\n");
    $tmpdir = $tmpdir;
    }
     
    Random Task, Feb 21, 2006
    #1
    1. Advertising

  2. Random Task <> wrote in news:1SHKf.1285$XZ3.142971
    @news20.bellglobal.com:

    > Hi i am trying to get $tmpdir to contain the "directory" instead of a
    > fully pathed file name ... i.e. truncate the file name.
    >
    > I am trying the below code ... but $1 is always empty ... help please

    :)

    Why are you reinventing the wheel?

    See

    http://search.cpan.org/~nwclark/perl-5.8.8/lib/File/Basename.pm

    See also splitpath and splitdir in

    http://search.cpan.org/~kwilliams/PathTools-3.16/lib/File/Spec.pm

    There is immense value to doing this kind of thing portably.

    > my $tmpdir = $file;
    > if ( $tmpdir =~/([.]*)[\\]{1}/)


    What are you fascinated with square brackets

    Do you know what [.]* means? Dot is not special in a character class. So
    [.]* means "match a dot zero or more times" before matching a character
    class containing a slash exactly one time. Of course, you need not
    specify a quantifier if you only want something to match exactly once.

    The regex automatically matches nothing followed by a slash in the path
    name, and nothing is captured as you specified.

    You need to read perldoc perlre.

    You also need to anchor the regex to the end of the path:

    #!/usr/bin/perl

    use strict;
    use warnings;

    chomp(my @paths = <DATA>);

    for my $p ( @paths ) {
    if ( $p =~ m{ \A ( .+ (?:/|\\) ) }x ) {
    print "$1\n";
    }
    }

    __DATA__
    C:\
    C:\Home\asu1\Dload\gvim.exe
    C:\Home\asu1\src\usenet\.test
    But\Please\use\File\Spec\.

    Sinan
     
    A. Sinan Unur, Feb 21, 2006
    #2
    1. Advertising

  3. Random Task

    Guest

    Random Task wrote:
    > Hi i am trying to get $tmpdir to contain the "directory" instead of a
    > fully pathed file name ... i.e. truncate the file name.


    For that task, I'd recommend using the dirname() function from the
    File::Basename module. It's standard, so you should aready have it.
    Read about it by typing "perldoc File::Basename" at the command prompt.

    Here's a quick code sample that does exactly what you want:


    #!/usr/bin/perl
    use strict;
    use warnings;
    use File::Basename;

    print "Type a filename: ";
    my $file = <STDIN>;

    # This next line is the line that does what you want:
    my $tmpdir = dirname($file);

    print "The directory is: $tmpdir\n";

    __END__


    By using the File::Basename module, you remove any need for regular
    expressions and any dependence on code that hasn't already been tested
    by many people in the Perl community. The code that does what you want
    in the sample program above is only one line long, so why write our own
    (longer) code that may initially suffer from several bugs?

    > I am trying the below code ... but $1 is always empty ... help please :)
    >
    > my $tmpdir = $file;
    > if ( $tmpdir =~/([.]*)[\\]{1}/)
    > {
    > print("MATCH\n");
    > $tmpdir = $1;
    > print ("[--->]".$1."<----]\n");
    > }
    > else
    > {
    > print("NO MATCH\n");
    > $tmpdir = $tmpdir;
    > }


    Uh... what is the purpose of this line, exactly?:

    > $tmpdir = $tmpdir;


    Do you know what this line even does? If so, why did you put it in?

    To be honest, it seems like you are fairly new to Perl programming.
    I would recommend purchasing (or checking-out from your local library)
    a book named "Learning Perl" (published by O'Reilly). It makes the
    assumption that the reader does not know any Perl programming, and
    teaches the beginner Perl programmer many programming concepts that all
    decent Perl programmers are expected to know (like regular expressions,
    which you seem to want to use but don't know how to use correctly).

    Personally, I don't think any programmer has become a "real Perl
    programmer" until he/she is familiar with at least 90% of that book.
    And I'm guessing that you never read that book, otherwise you wouldn't
    have made some of the regular expression mistakes that you did.

    Don't feel bad if you've never read the "Learning Perl" book. Every
    one of us, at some point in our lives, had not read that book. Pick it
    up, read it, work through the examples, and you'll find that you can
    read and write Perl code much easier and better than ever before.

    I hope this helps.

    -- Jean-Luc
     
    , Feb 21, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PHaroZ
    Replies:
    5
    Views:
    94,825
    akash02sap
    Aug 17, 2012
  2. Steve Singer
    Replies:
    0
    Views:
    542
    Steve Singer
    Feb 15, 2005
  3. Replies:
    26
    Views:
    1,035
    Lew Pitcher
    Jun 4, 2007
  4. dude
    Replies:
    5
    Views:
    1,715
    Jussi Piitulainen
    Mar 17, 2011
  5. RVince
    Replies:
    9
    Views:
    422
    Dave Angel
    May 25, 2011
Loading...

Share This Page