Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by u914 (Pilgrim)
on Jun 04, 2002 at 19:16 UTC ( [id://171607]=note: print w/replies, xml ) Need Help??


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

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!)

Replies are listed 'Best First'.
Re: Re: Re: Create a list of directories without . and ..
by dsheroh (Monsignor) on Jun 04, 2002 at 19:37 UTC
    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.
      

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

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://171607]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found