in reply to Re: Create a list of directories without . and ..
in thread Create a list of directories without . and ..

-d $_ and ! /^\.\.?/
Watch it. That falsely excludes ..foo and .bar. Oops! Maybe you want:
-d $_ and $_ ne "." and $_ ne ".."
Sometimes a regex is overkill.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: •Re: Re: Create a list of directories without . and ..
by dws (Chancellor) on Jun 04, 2002 at 22:34 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.
      

Re: •Re: Re: Create a list of directories without . and ..
by Juerd (Abbot) on Jun 04, 2002 at 22:27 UTC

    Sometimes a regex is overkill.

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

    Less readable, but faster and shorter.

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

        Are you sure it's faster?

        I have benchmarked this before. It is faster. With short matches, regexes are often faster. /^#/ is faster than substr($_, 0, 1) eq '#'. (all assuming you'll have more non-matches than matches, which is true when checking when checking for . and ..)

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