Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Parent directory - regex

by jevon (Initiate)
on Jun 17, 2004 at 06:40 UTC ( #367548=perlquestion: print w/replies, xml ) Need Help??

jevon has asked for the wisdom of the Perl Monks concerning the following question:

I found dirname

oops. Forget it!

Hello Monks,

Given the string "/dir1/dir2/dir3/ .. /dirLast//"

is stored in a variable $myDirectory

How do I extract the entire path up to "/dirLast"?

Put simply, I want the path to the parent directory
of $myDirectory.

Thanks
Jevon

Replies are listed 'Best First'.
Re: Parent directory - regex
by gawatkins (Monsignor) on Jun 17, 2004 at 12:05 UTC

    Here is an example of a simple script to do this without using regex:

    #!\usr\bin\perl use strict; my $myDirectory = "/dir1/dir2/dir3/dir4/dirLast//"; my @myDir = split('/', $myDirectory); #creates an array of Dir names pop @myDir; #removes the currnet directory $myDirectory = join( '/', @myDir); #Joins names back into parent path print $myDirectory;

    Thanks,
    Greg W
Re: Parent directory - regex
by davidj (Priest) on Jun 17, 2004 at 12:39 UTC
    Here's a way to do it with a regex

    #!\usr\bin\perl use strict; my $myDirectory; my $parent; $myDirectory = "/dir1/dir2/dir3/dir4/dirLast//"; # with '/' after dirLast ($parent) = $myDirectory =~ m#(.+)/#; print "$parent\n"; # without '/' after dirLast ($parent) = $myDirectory =~ m#(.+)//#; print "$parent\n"; exit;

    davidj
Re: Parent directory - regex
by pbeckingham (Parson) on Jun 17, 2004 at 13:06 UTC

    With a regex, I'd do this, but I would ordinarily use File::Basename to do this:

    my ($parent) = "/dir1/dir2/dir3/ .. /dirLast//" =~ m#^(.*)/.*?//$#;

Re: Parent directory - regex
by periapt (Hermit) on Jun 17, 2004 at 12:30 UTC
    There are many ways to do this sort of thing. If your path is always known in a string then one simple approach is this
    my $path = "/dir1/dir2/dir3/ .. /dirn-1/dirLast//"; my @pathary = split(/\//,$path); my $parentpath = join('/',@pathary[0..$#pathary-1]); # pop @path; # also # my $parentpath = join('/',@pathary); # works print "$path\n$parentpath\n";
    An alternative, if the directory your are in is unknown or changes in an unpredictible manner, might be something like this. Note: This is rather inefficient but works.
    use Cwd; my $oldpath = getcwd(); print "$oldpath\n"; $path = '/var/tmpdir'; chdir $path; $path = getcwd(); # for info only print "$path\n"; # for info only chdir "../"; $path = getcwd(); print "$path\n";

    PJ
    We are drowning in information and starving for knowledge - Rutherford D. Rogers
    What good is knowledge if you have to pull teeth to get it - anonymous

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://367548]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2019-09-22 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (274 votes). Check out past polls.

    Notices?