You have a logic error in your code somewhere. Your logic for counting the 
files with a name matching a certain pattern is too complicated to follow and 
point out where your error lies and how to fix it. The basic problem is that 
your logic depends upon detecting when the directory name changes, and will 
therefore not work on the last file encountered. It also depends upon all of 
the files in a directory being scanned sequentially. While that seems to work, 
it is not guaranteed by File::Find.

A simpler method for counting files in each directory would be to use a hash 
with the directory name as key and the number of files as value. Here is a 
program for doing that:

#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;

my $startdir = './dir1';
my %counts;

find sub {
        return unless -f;
        return unless /^\d+$/;
        $counts{$File::Find::dir}++;
}, $startdir;

my( $total_dir, $total_files);
print "# Files   Directory\n-------   ---------------\n";
for my $dir ( sort keys %counts ) {
        (my $shortdir = $dir) =~ s{.*News/agent/nntp}{};
        my $nfiles = $counts{$dir};
        printf "%5d     %s\n", $nfiles, $shortdir;
        $total_dir++;
        $total_files += $nfiles;
}
print "\n<$total_files> files in <$total_dir> directories\n”;


Note that I recommend keeping the raw numerical data (file counts) and not 
saving the output lines. Generate the output lines when you print them.


Jim Gibson

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to