Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: special directory entries

by jwest (Friar)
on Dec 04, 2001 at 01:12 UTC ( #129183=note: print w/ replies, xml ) Need Help??

in reply to special directory entries

You're better off to just strip . and .. out as they come in. Something like:

@dirs = grep(!/^\.\.?$/, readdir(DIR));
would do it nicely.

Hope this helps!


-><- -><- -><- -><- -><-
All things are Perfect
    To every last Flaw
    And bound in accord
         With Eris's Law
 - HBT; The Book of Advice, 1:7

Comment on Re: special directory entries
Download Code
Replies are listed 'Best First'.
Re:x2 special directory entries
by grinder (Bishop) on Dec 04, 2001 at 03:19 UTC
    Why do people keep trotting out this readdir-and-regexp idiom to deal with current and parent directories?

    @dirs = grep($_ ne '.' and $_ ne '..', readdir(DIR));

    Sure it's a little more verbose, but it doesn't suffer from sneaky boundary failures (that blakem points out elsewhere in this thread). While it's more verbose, it's also more efficient, as it doesn't need regexp machinery.

    Of course, that should be File::Spec::curdir and File::Spec::updir, to be on the safe side.

    To find out FMTYEWTK about readdir, '.' and '..', I recommend reading this thread, especially merlyn's reply.

    g r i n d e r
    just another bofh
Re: Re: special directory entries
by cybermack72 (Novice) on Dec 04, 2001 at 02:20 UTC
    Anything for Win32...?
      As far as I know, this should be portable to Win32. Unless grep isn't implemented there, but I believe it to be. If you let me know where this fails on Win32, I might be able to help.

      Also, wog's solution below looks like it will handle any portability issues transparently.


      -><- -><- -><- -><- -><-
      All things are Perfect
          To every last Flaw
          And bound in accord
               With Eris's Law
       - HBT; The Book of Advice, 1:7

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2015-11-27 03:47 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (717 votes), past polls