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

This is a bit LISPy, but it will work:

chdir "/home/www"; opendir DIR, "."; my @third_level_dirs = ( # 3. This tests for their presence grep { -d } ( # 2. This adds /logs to the end map { "$_/logs" } ( # 1. this returns all directories but . & .. grep { !/^\.\.?$/ && -d } (readdir DIR) ) ) ); closedir DIR;

Replies are listed 'Best First'.
Re^2: How do I put all sub-directories into an array?
by Aristotle (Chancellor) on Jul 23, 2002 at 13:55 UTC
    grep { !/^\.\.?$/ && -d }
    Careful: a directory called ..\n would fall through the cracks using $ as "end of string" match. You want grep { !/\A\.\.?\z/ && -d } But we don't have to use a regex for everything. grep { $_ ne '.' and $_ ne '..' and -d } ____________
    Makeshifts last the longest.
Re: Re: How do I put all sub-directories into an array?
by wylie (Novice) on Jul 24, 2002 at 08:40 UTC

    Thanks for your help Aristotle.

    Here's what I ended up with:

    chdir "/home/www"; opendir DIR, "."; my @third_level_directories_only= ( grep { $_ ne '.' and $_ ne '..' and -d } (readdir DIR) ); closedir DIR;

    This puts the names of all my third level directories (my virtual hosts) into any array.