Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by BrowserUk (Pope)
on Jun 04, 2002 at 18:52 UTC ( #171599=note: print w/ replies, xml ) Need Help??


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

Being unsure of the etiquette here, I am not sure if "thankyou"s are in order (but no doubt someone will tell me...:)

So, until that piece of enlightenment comes my way... Thankyou!!

Living in darkness; seeking the light; Weak, but will be strong with the help of the Brothers


Comment on Re: Create a list of directories without . and ..
Re: Re: Create a list of directories without . and ..
by 914 (Pilgrim) on Jun 04, 2002 at 19:16 UTC
    you can also use File::Find (as i've recently found)

    something like: (i've not tested this!)

    my @found; File::Find::find({wanted => \&wanted], '.'); sub wanted { if (-d $_) && ($_ != '.') && ($_ != '..') { push(@found, $_) } }
    sure, this is not the fastest way (someone on the above referenced node timed the native find vs File::Find, with native winning by a factor of 5), but it's a way.

    also, be careful of the greps to remove the . and .. entries... it's legal to have directories that start with a period (or two, or more), so you have to do an exact match on both cases.

    (i think)

    i too am a newbie here, and i've been thanking people...
    hopefully, i can provide some answers myself one day.

    (ps: please forgive the expanded syntax, but it's (to my unPerlish eye) easier to read!)

      You are correct about . and .. - getting a regex to match them reliably is far trickier than it looks. The obvious m/^\.\.?$/, for instance, will give a false positive on "..\nsubdir". Much safer and simpler to just test on ($_ ne '.') && ($_ ne '..'). (BTW, != is numeric. Use ne for strings.)

        The obvious m/^\.\.?$/, for instance, will give a false positive on "..\nsubdir"

        No, only on "..\n" and ".\n".

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

      if (-d $_) && ($_ != '.') && ($_ != '..') {

      This is invalid syntax (needs an extra set of parens) and the != operator is not the one you want for string equality tests. I think you meant:

      if (-d and $_ ne '.' and $_ ne '..') {
      -d uses $_ if no argument is given.

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

        Hi. Still having trouble with this...I do not understand why calling "test.pl ." finds subdirs in the Products subdir if I run it there and pass "." as the arg but doesn't find them if I run it one level back (ie. CD ..)and pass "./Products" as the arg??

        This is true which ever of the two filter clauses I use.

        I hate being a "newbie" again.

        C:\www\NB.biz\html>type test.pl #!/usr/bin/perl -w use strict; use diagnostics; print $ARGV[0], "\n"; opendir(DIR, $ARGV[0] ) or die ".: $!"; #my @list = grep { -d and /^\.\.?/ } readdir(DIR); my @list = grep { -d $_ and $_ ne '.' and $_ ne '..'} readdir(DIR); closedir(DIR); print foreach ( @list ), "\n"; C:\www\NB.biz\html>test.pl . . cssgraphicsProducts C:\www\NB.biz\html>test.pl ./products ./products <<<< NO OUTPUT HERE??? C:\www\NB.biz\html>test.pl ./Products/ ./Products/ <<<< OR HERE???? C:\www\NB.biz\html>cd products C:\www\NB.biz\html\Products>../test.pl . . Books C:\www\NB.biz\html\Products>
        Thanks, NJ Sandever.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2015-07-04 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls