On Jun 28, 2017, at 2:10 PM, Harry Putnam <rea...@newsguy.com> wrote:
> 
> 1) I want to count numeric named files in each directory.
> 
> 2) I want to capture the name of the directory those files are in
> 
> 3) I want to print the directory name and the count (if any) for each
>  directory.
> 
> I know that all the information I want to extract is available in
> File::Find.
> 
> Just having trouble seeing how to get at it when I want it.
> 
> Connecting the directory name with the file count is the rub for me.
> 
> I googled extensively but have not found this specific usage.  I get
> piles of examples for just counting files throughout a hierarchy.
> But not giving a count and directory name of `each' directory.
> 
> I've done several hours of fumbling around and have produced several
> abject failures.
> 
> Finally got something that did what I wanted.
> 
> The script below does those three things... But I have a sneaking
> feeling there is some easier way to extract the info.  Something
> simpler that is right in front of me... but escaping me.
> 
> The test directory structure looks like this:
> 
> ls -R dir1
> dir1:
> 111  222  333  dir2
> 
> dir1/dir2:
> 111  222  333  dir3
> 
> dir1/dir2/dir3:
> 111  222  333
> 
> ff1c:
> -------       -------       ---=---       -------       -------
> 
> my $usage =
> "Purpose: bleh
> Usage: bleh
> ";
> 
> my $dir;
> if (! @ARGV){
>  warn "$usage",
>   "Usage tripped: line: <" . __LINE__ . ">\n",
>   "We need at least 1 top directory\n";
>  exit;
> } elsif (@ARGV > 1) {
>  warn "$usage",
>   "Usage tripped: line: <" . __LINE__ . ">\n",
>   "Too many cmdline arguments\n";
> }
> 
> $dir = shift;
> if (! -d $dir) {
>  warn "$usage",
>   "Usage tripped: line: <" . __LINE__ . ">\n",
>   "<$dir> not found .. aborting ..\n";
>  exit;
> }
> -------       -------       ---=---       -------       -------
> 
> ff1c out:
> 
>> ./ff1c
> 
> ./dir1            3
> ./dir1/dir2       3
> ./dir1/dir2/dir3  3
> (9) numeric files overall

I don’t see how the code you have posted can possibly produce the output you 
have reported. The code above looks like error checking that the user of the 
program has entered one argument, and that argument is the name of a directory.

How about posting the rest of your program. The error checking part is the 
least interesting part, as it is very straightforward and although it is a 
valuable part of any good program, it is not relevant to the discussion of how 
to count numerically-named files. So how about posting a program that includes 
only the part of Perl that you want people to evaluate.

If I had to do this, I would create a hash that had the directory name as a key 
and the number of files in that directory that have a numerical file name. I 
would use File::Find to traverse the directory tree, check the name of each 
non-directory file, and increment the count for the enclosing directory for 
each such file I found.

A line something like this:

        $filecount{$File::Find::dir}++;

would be central to my program.




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