Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Depth Listing in Directory Traversal

by hbo (Monk)
on Jul 20, 2004 at 04:36 UTC ( #375796=note: print w/replies, xml ) Need Help??

in reply to Depth Listing in Directory Traversal

sub next { my $self = shift; while( 1 ) { if ( @{ $self->{output} } ) { my $line = shift @{ $self->{output} }; return $line; }
Since we see no other references to this array ref, I'm assuming this code is cut down from its full version.
if ( ! @{ $self->{dirs} } ) { return; } my $dir = shift @{ $self->{dirs} }; if (-d $dir) {
Add a proper declaration for $olddir above, and:
if ($oldir){ $self->{level} += ($dir=~tr!/!/!) - ($olddir=~tr!/!/!); } $olddir=$dir;
I haven't tested this, but the idea is that, since the paths are all absolute, a change in level means a difference in the number of path seperators. (You might want to use a more portable form of the seperator than a literal "/".)

Replies are listed 'Best First'.
Re^2: Depth Listing in Directory Traversal
by crabbdean (Pilgrim) on Jul 20, 2004 at 04:56 UTC
    Well I'm not sure this will work because the code makes the program go down the directory first, not across. Based on your idea it would have to keep a record somehow of the $oldir variable for when it finally finished going down the tree and came back to start on the next level. This requires it to keep a kind of memory of the tree and what it has or hasn't done. There must a simpler way.

    BUT ... your idea has merit and something I'd considered also - your post has made me rethink it. If the number of path seperators is greater than $depth then don't push to the stack. That may work. I just don't like using relying on counting "\" in strings as a reliable method of keeping track of depth. If by chance a directory has that character in it, the routine will fail.

    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
      On Windows,"\" is the path seperator. A file name can't contain that character, so you won't encounter it unless it means a depth transition in the stack. Same goes for "/" on Unix. The tricky part with Windows would be a rooted path like "c:\\". For that you could count occurrences of "\+",although I believe that ActivePerl translates the seperators to "/" for portability with Unix.

      The "width first" traversal of the directory tree doesn't matter in the code I gave. The path seperator count will be the same between peer directories, thus their difference will be zero, and we will add zero to the depth counter. This also works for transitions up the stack, since that difference will be negative.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375796]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2017-01-21 03:42 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (181 votes). Check out past polls.