One neat perl trick is the ability to push elements onto an array while you iterate over it. Note don't try to do anything but push unless you are feeling *very* lucky. For example you can 'recurse' the file structure with a handful of lines:
sub recurse_tree { my @dirs = @_; my @files; for my $dir ( @dirs ) { opendir DIR, $dir or error("Can't open $dir\n"); while ( my $file = readdir DIR ) { next if $file eq '.' or $file eq '..'; next if -l "$dir/$file"; push @dirs, "$dir/$file" if -d "$dir/$file"; push @files, "$dir/$file" if -f "$dir/$file"; } closedir DIR; } return \@dirs, \@files; }
One of the intersting features of this algorithm is that it proceeds width first, rather than depth first as with most recursive algorithms.
cheers
tachyon
In reply to Re: Traversing a simple tree
by tachyon
in thread Traversing a simple tree
by nightwatch
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |