DS has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, how can I get the names of the subdirectories under a one directory ,, I just what to take the names and store them in an array ..
my @names = glob("c:/mainDir/*");
thanks

Replies are listed 'Best First'.
Re: getting the names of directories under a directory
by larryk (Friar) on Aug 29, 2002 at 21:18 UTC
    The file test operator -d works by default on $_ so you can simply grep the glob result before storing in an array.
    my @names = grep -d, glob("c:/mainDir/*"); #or even my @names = grep -d, <c:/mainDir/*>;
       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
    
      thanks :)
Re: getting the names of directories under a directory
by greenFox (Vicar) on Aug 30, 2002 at 00:56 UTC
    Another option is perlfunc:readdir you should have enough information from larryk's post to adapt the example in the man page. And of course if you want to do this recursively you should be looking at File::Find.

    --
    Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

Re: getting the names of directories under a directory
by simeon2000 (Monk) on Aug 30, 2002 at 03:29 UTC
    I just love DirHandle, myself.

    use DirHandle; my $dh = new DirHandle('/etc') or die "Can't open /etc: $!"; my @subdirs = grep { -d && $_ !~ /^\.?\.$/ } $dh->read(); $dh->close();
    @subdirs now has what you want.

    --
    perl -e "print qq/just another perl hacker who doesn't grok japh\n/"
    simeon2000|http://holdren.net/