Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Create a list of directories without . and ..

by dws (Chancellor)
on Jun 04, 2002 at 18:34 UTC ( [id://171595]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://171595]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found