in reply to How do I put all sub-directories into an array?

You could use File::Find:
use File::Find; my @DIR; my $dir = '/home/www/'; find( \&wanted , $dir); sub wanted { push(@DIRS, $File::Find::dir."/".$_) if -d $_; }
I guess that's what you wanted right? If you don't want the absolute path just do push(@DIRS, $_) if -d $_ instead.

giant

Replies are listed 'Best First'.
Re: Re: How do I put all sub-directories into an array?
by wylie (Novice) on Jul 23, 2002 at 10:44 UTC
    Thanks

    That's almost what I want but lists all the sub-directories below /home/www.

    What I actually want is the third-level directories only i.e. the name_of_virtual_host directories only in the array, not all the directories below this level too.

    My directory structure is /home/www/name_of_virtual_host/dir4/dir5/etc.

    Can I use finddepth here to recurse down to the third level directories only?

    Wylie
      well in this case I would simply do something like:
      opendir(DIR, '/home/www/'); while (my $dir = readdir(DIR)) { print $dir; next if $dir =~ /^\.\.?$/ || ! -d $dir; push(@dirs, $dir); } closedir(DIR);
      => Code is untested but should work.

      giant
        next if $dir =~ /^\.\.?$/ || ! -d $dir;
        Since you never fix up $dir to be the path including the directory name, this test is guaranteed to fail, except under the random moment when a directory below /home/www is also in the current directory. See the other solutions in this thread for working examples, of which yours is not.

        Code is untested but should work.
        Where "should" here means "I would like it to", not "it does". {grin}

        -- Randal L. Schwartz, Perl hacker

        Giant, thanks for all your help with this.

        In the end I used a modification of mugwumpjism's suggestion to get to where I wanted to be.

        Here's the code:

        chdir "/home/www"; opendir DIR, "."; my @vhosts= ( # 1. This tests for the presence of directories grep { -d } ( # 2. this returns all directories but . & .. grep { !/^\.\.?$/ && -d } (readdir DIR) ) ); closedir DIR;
        This sticks all my virtual hosts in an array :).
Re^2: How do I put all sub-directories into an array?
by Aristotle (Chancellor) on Jul 23, 2002 at 13:52 UTC
    Why flagellate yourself writing $File::Find::dir."/".$_ when there's $File::Find::name? :)

    Makeshifts last the longest.

      Historically that's what novice monks liked to do (grin).