Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to Get the Last Subdirectories

by pemungkah (Priest)
on Mar 12, 2010 at 00:50 UTC ( #828175=note: print w/ replies, xml ) Need Help??


in reply to How to Get the Last Subdirectories

I started by setting up a sample set of test directories:

[mcmahon@joe-desk ~]$ ls -R ./example/ ./example/: file nonempty_files_only nonempty_has_dirs ./example/nonempty_files_only: file1 file2 ./example/nonempty_has_dirs: file1 one two ./example/nonempty_has_dirs/one: ./example/nonempty_has_dirs/two:
That's a directory containing files and other (nonempty) directories, one containing only files, and one containing a file and two empty directories.
sub dive { my($d) = shift; return if ! -d $d; my @contents = glob("$d/*"); return $d unless @contents; my @below = map { dive($_) } @contents; return @below ? @below # Stuff below qualifies, this doesn't : $d; # Nothing below qualifies, this does } $d = './example'; print join ", ", dive($d),"\n";
This prints
./example/nonempty_files_only, ./example/nonempty_has_dirs/one, ./exam +ple/nonempty_has_dirs/two
The tricky bit is postponing the decision about whether the current directory is good until you've seen if any subdirectories of it qualify.

Edit: Removed the majority of the comments as they were actually obscuring how short this is; renamed @queue as it was a leftover from a previous, longer, iterative version.


Comment on Re: How to Get the Last Subdirectories
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-08-22 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (148 votes), past polls