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

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

Replies are listed 'Best First'.
•Re: Re: Re: Re: How do I put all sub-directories into an array?
by merlyn (Sage) on Jul 23, 2002 at 12:49 UTC
    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

      hi merlyn,
      sorry I was in a hurry when writing this one, of course it would fail if /home/www issn't the current dir :)

      giant
Re: Re: Re: Re: How do I put all sub-directories into an array?
by wylie (Novice) on Jul 24, 2002 at 08:21 UTC

    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 :).