Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

List directory(s)

by Anonymous Monk
on May 23, 2008 at 06:36 UTC ( [id://688093]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,
I want to list the directory tree from the full file name.
Example: $filename=/usr/local/bin/perl I have to display a tree like this 1. /usr/ 2. /usr/local/ 3. /usr/local/bin/

Is there any module or any shohtcut to do this ?.
Thanks.

Replies are listed 'Best First'.
Re: List directory(s)
by grinder (Bishop) on May 23, 2008 at 07:33 UTC
    Is there any module or any shohtcut to do this ?

    Yup, File::Basename is what you're looking for. The idea is to strip off the last component of the path (by isolating the directory component) until you walk up high enough in the file system.

    Note that I would have considered your example to start with 1. /, the root directory, but that's just me. Anyway, in a nutshell the code will look something like:

    #! /usr/local/bin/perl use strict; use warnings; use File::Basename 'dirname'; my $path = shift || '/usr/local/bin/perl'; my @ancestor; while (1) { $path = dirname($path); last if $path eq '/'; push @ancestor, "$path/"; } print "$_\n" for reverse @ancestor;

    • another intruder with the mooring in the heart of the Perl

Re: List directory(s)
by mwah (Hermit) on May 23, 2008 at 07:30 UTC

    What do you *really* want to do? What's the purpose? Maybe there's a better version than the simplistic approach:

    ... my $filename = '/usr/local/bin/perl'; # get tree parts my @parts = $filename =~ m{ [^/]+ (?=/) }xg; # build tree for my $p (0 .. $#parts) { printf "%d. /%s/\n", 1+$p, join('/', @parts[0..$p]) } ...

    Regards

    mwa

Re: List directory(s)
by roboticus (Chancellor) on May 23, 2008 at 11:35 UTC
    Anonymous Monk:

    (Jumpin' Jiminy--you sure post a lot!)

    Here's another way to do it. (I just saw your question, and thought I'd try array slices to do the job.)

    #!/usr/bin/perl -w use strict; use warnings; my $fname = '/usr/local/bin/perl'; my @fname_parts = split /\//, $fname; # Start at 1 to skip blank directory part (chunk before /usr), # and @fname_parts-2 to leave off the final chunk (program name) for my $i (1 .. @fname_parts-2) { print "$i. ", join('/', @fname_parts[0..$i]), "\n"; }
    ...roboticus
Re: List directory(s)
by jeepj (Scribe) on May 23, 2008 at 08:22 UTC
    not tested, written on the fly:
    my $filename="/usr/local/bin/perl"; my $accu=""; my $nb=0; map{$nb++;$accu.="/".$_;print "$nb. $accu\n";} split(/\//, $filename);
Re: List directory(s)
by MidLifeXis (Monsignor) on May 23, 2008 at 16:43 UTC

    Or a recursive solution.....

    use strict; use warnings; sub worker { my $dirsep = shift; worker ($dirsep, @_[0 .. $#_ - 1]) if ($#_ > 1); # terminator print join($dirsep, @_), "\n" } worker("/", split("/", "/usr/bin/foo/bar/baz"));
    TIMTOWTDI

    --MidLifeXis

Re: List directory(s)
by mwah (Hermit) on May 23, 2008 at 20:01 UTC

    or an obfuscated solution:

    my $filename = '/usr/local/bin/perl'; open my $fh, '<', \$filename; local ($/,$\) = ('/')x2; /(?<=\w)\// && ($\.=$_) && print("\n".($.-1).'. ') while <$fh>;

    Regards

    mwa

Re: List directory(s)
by carol (Beadle) on May 23, 2008 at 20:29 UTC
    while ( $filename =~ /(.+?\w+\/)/g ) { print $`, $1, $/; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found