in reply to Hash problem (possible recursion)
Whenever I see that kind of feature shift, I worry. Is it the case that the customer really does not want the original information anymore, and only wants the modified information? Maybe the new spec was meant to supplement the existing data, instead of replacing it -- i.e. something like:
$hash{/path/to/dir1}{dir} = number of directories in dir1
$hash{/path/to/dir1}{sum} = sum of all subdirectories under dir1
...
The point is that the original information might still be useful; hopefully, the customer can be clear about this.
When you say you "read in" the hash, it might make things easy if we knew what you were reading from (a list file? output from "find /path -type d"? File::Find?). Rather than having the hash loaded one way and then retooled to be another way, you should be able to load it and have it be the way you want in one pass -- use a technique similar to what ikegami proposed above, but do it while the hash is being loaded.
UPDATE: In case it helps, here's a sample that reads from a "find -d" command, and tabulates the both your original stats (number of directories immediately contained in each directory) and the new stats (total number of subdirectories subsumed under each directory):
Note the line after "chomp" that is commented out: if you uncomment that line, the output will list all directories, including the "terminals" (those having no further subdirectories, yielding zeros for "dir" and "sum"); by commenting it out, the listing only contains the "non-terminal" directories.#!/usr/bin/perl use strict; die "Usage: $0 [path ...]\n" if ( @ARGV and not -d $ARGV[0] ); push @ARGV, "." if @ARGV == 0; my %hash; $/ = "\0"; open( FIND, "-|", "find", @ARGV, qw/-type d -print0/ ) or die "can't run find @ARGV: !$\n"; while ( <FIND> ) { chomp; # $hash{$_}{dir} = 0; next unless ( s{/[^/]+$}{} ); $hash{$_}{dir}++; $hash{$_}{sum}++; while ( s{/[^/]+$}{} ) { $hash{$_}{sum}++; } } printf "%6s %8s %s\n", qw/dirs subdirs path/; for ( sort keys %hash ) { printf "%6d %8d %s\n", $hash{$_}{dir}, $hash{$_}{sum}, $_; }
(updated code to remove a redundant use of m{})
|
|---|