SysAdm has asked for the wisdom of the Perl Monks concerning the following question:

I currently read a directory and add the total files in the directory into $numfiles. I would now like to IGNORE any files in the directory that DO NOT have a DIGIT in the file name... I want to only count the files with DIGITS in the file name...

Current code:

opendir DIR, "$config{'basepath'}$key" or &oops("Category directory $k +ey could not be opened."); readdir DIR;readdir DIR; my $numfiles = scalar @{[readdir DIR]}; closedir DIR;


I appreciate everyone's last response to my perl problem... I'm stuck on where to compare /d on this one.

______SysAdm

Replies are listed 'Best First'.
(ar0n) Re: Counting only certain files...
by ar0n (Priest) on May 17, 2001 at 21:19 UTC
    my $numfiles = grep { /\d/ } glob("$config{basepath}/*");

    ar0n ]

Re: Counting only certain files...
by SilverB1rd (Scribe) on May 17, 2001 at 21:23 UTC
    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
      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;
      --perlplexer
        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!)

        _______SysAdm
Re: Counting only certain files...
by blue_cowdawg (Monsignor) on May 17, 2001 at 22:10 UTC

    Here's one of many ways....

    #!/usr/bin/perl -w ################################################################## use strict; opendir(DIR,"."); my @list=grep /^.*[0-9].*$/, readdir(DIR); my $numfiles=scalar(@list); printf "%d files\n",$numfiles; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Peter L. Berghold --- Peter@Berghold.Net "Those who fail to learn from history are condemned to repeat it."