Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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!

--jwest

-><- -><- -><- -><- -><-
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
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.

      --jwest


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-12-25 03:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls