Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Counting only certain files...

by SilverB1rd (Scribe)
on May 17, 2001 at 21:23 UTC ( #81304=note: print w/replies, xml ) Need Help??

in reply to Counting only certain files...

I would suggest passing readir to a grep.
I used it in one of my scripts like so,
@Files = grep {not /^(\.\.?|list\.txt)$/} readdir(DIR);
This returned all files except '.' and '..' and a file named 'list.txt'.

The Price of Freedom is Eternal Vigilance

Replies are listed 'Best First'.
Re: Re: Counting only certain files...
by perlplexer (Hermit) on May 17, 2001 at 21:37 UTC
    I think using grep() or glob() in this case is a total overkill. You are not going to use filenames afterwards, so there is no need to allocate all that memory just to throw away the result.
    my $total = 0; opendir DIR, "$config{'basepath'}$key" or &oops("Category directory $k +ey could not be opened."); while ($_ = readdir DIR){$total++ if -f and /\d/o } closedir DIR;
      I went with this...

      my $numfiles = 0; opendir DIR, "$config{'basepath'}$key" or &oops("Category +directory $key could not be opened."); readdir DIR;readdir DIR; while ($_ = readdir DIR){$numfiles++ if /\d.dat/o } closedir DIR;

      My files actually end with .dat, therefore I added the extension...

      I want to thank you all for your help... GOD I love this site... (no pun intended!)


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-08-15 07:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found