Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How to Get the Last Subdirectories

by Anonymous Monk
on Mar 11, 2010 at 21:05 UTC ( #828137=note: print w/ replies, xml ) Need Help??


in reply to How to Get the Last Subdirectories

What have you tried?


Comment on Re: How to Get the Last Subdirectories
Re^2: How to Get the Last Subdirectories
by bichonfrise74 (Vicar) on Mar 11, 2010 at 21:36 UTC
    Unfortunately, I have not tried anything yet. It's more like I am just thinking of how I should do it. Below is the idea that I was thinking.

  • Create a recursive hash and store each subdirectory as a key.
  • Loop through the recursive hash and place the 'directory' in a new hash. So, if the key is already in the new hash, then continue to the next one.

    But the above idea might be making things complicated. I was wondering if there is a simple approach to this or I may just be over complicating the problem.
      You are basically interested in those directories in your tree which have no subdirectories; so the following algorithm should work:
      1. Initially create an empty hash %leaf_directories
      2. Whenever File::Find drops you into a directory $d, do the following:
        1. Remove the parent directory of $d from the hash, i.e. if $d contains the full path, do a delete $leaf_directories{dirname($d)}. Of course this will fail occasionally (because there is no corresponding entry), but we ignore this.
        2. Add $d to your hash, i.e. $leaf_directories{$d}=1
      In the end, keys $leaf_directories should be the list of the directories without subdirectories.


      -- 
      Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-09-21 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls