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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.