in reply to Create a list of directories without . and ..

I want @list to contain a list of all directories in "." excluding "." and ".."?

Try this:

opendir(DIR, ".") or die ".: $!"; my @list = grep { -d $_ and ! /^\.\.?/ } readdir(DIR); closedir(DIR);
The anonymous list part is left as an exercise.

Replies are listed 'Best First'.
•Re: Re: Create a list of directories without . and ..
by merlyn (Sage) on Jun 04, 2002 at 22:20 UTC
      Watch it. That falsely excludes ..foo and .bar. Oops! Maybe you want ...

      Ewps. Typo on my part. I meant

      -d $_ and ! /^\.\.?$/ ^
      though \z would be safer than $.

        -d $_ and ! /^\.\.?$/

        No, you meant \z instead of $. $ matches _before_ \n (if there is any). Newline is a valid character in filenames with many filesystems.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.
        

      Sometimes a regex is overkill.

      !/^\.\.?\z/.

      Less readable, but faster and shorter.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Re: Re: Create a list of directories without . and ..
by webfiend (Vicar) on Jun 04, 2002 at 19:36 UTC

    Great suggestion (I ++ed it already). One little flaw that catches me all the time: it breaks as soon as you do apply it to any directory but "./"

    Easy to fix, though. Just be explicit about what directory you're opening.

    my $dir = "."; opendir(DIR, $dir) or die "$dir: $!"; my @list = grep { -d "$dir/$_" and ! /^\./ } readdir(DIR); closedir(DIR);

    "All you need is ignorance and confidence; then success is sure."-- Mark Twain