It seems like you are trying to use some kind of priority queue or stack or something with directories getting processed out and files being stuck in. So you take a directory out of the queue (lets say on the right), process the directory and stick the files in the queue (on the left) which are never taken out. You keep doing this until there are no more directories, then you have a list of file results? Interesting!
I made a recursive version before (I hope) I realized what was going on then tried to emulate what you were talking about.
#!/usr/bin/perl -w use Cwd qw/getcwd realpath/; use strict; my %processed; sub recursive { my $dirarg = shift; chdir $dirarg; my @results = map { chomp; realpath($_); } `ls $dirarg`; # ls is my example "vendor program" that returns files/dirs # prevent infinite recursion because of sym-links $processed{$dirarg} = 1; my @found = grep { -f } @results; # get list of files foreach my $dir ( grep { -d } @results ) { # same for directories push @found, recursive($dir) unless($processed{$dir}); } return @found; } sub queue { my @heap = ( shift ); my %processed = (); while(-d $heap[-1]) { my $dir = pop @heap; next if $processed{$dir}; chdir $dir; my @result = map { chomp; realpath($_); } `ls $dir`; $processed{$dir} = 1; unshift @heap, grep { -f } @result; push @heap, grep { -d } @result; } return @heap; } print join "\n", (recursive(getcwd()), "\n"); print join "\n", (queue(getcwd()), "\n");
In reply to Re: Unshift and push inside of map operation.
by juster
in thread Unshift and push inside of map operation.
by LNEDAD
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |