Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to Re: How to Get the Last Subdirectories by pemungkah
in thread How to Get the Last Subdirectories by bichonfrise74

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (7)
    As of 2014-12-25 19:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (162 votes), past polls