I've inherited some code...
A hash is read in with the following key format:
$hash{/path/to/dir1}{dir} = number of directories in dir1 (4 for example)
$hash{/path/to/dir1/sub1}{dir} = number of directories in sub1 (3 for example)
$hash{/path/to/dir2}{dir} = number of directories in dir2 (12 for example)

My customer now wants to see $hash{/path/to/dir1}{dir} contain the total number of directories underneath it (7 using example numbers), not just in the /path/to/dir1 directory. Data::Dumper has been used to store previous runs which gathered all this information. Anyone with suggestions on how best to solve this? I'm fairly comfortable with Perl for my basic admin scripting needs, but this reeks of something like recursion. Any help would be greatly appreciated.

Update

Here is the code that generates the current output...
use strict; use File::Find; use Fcntl ':mode'; my $cur_dir; my %stats; my ($files, $dirs, $links, $tfiles, $tdirs, $tlinks); my ($mode, $user, $group); my $depth = split('/', $ARGV[0]) + $ARGV[1]; find ( \&user_files, $ARGV[0]); print "Files: $tfiles\n"; print "Dirs: $tdirs\n"; print "Links: $tlinks\n"; printf "%-40s %-10s %-10s %-10s\n", 'Directory', 'Dirs', 'Files', 'Lin +ks'; print '-' x 70, "\n"; for ( sort keys %stats ) { $stats{$_}{dirs}-- if ( $_ eq $ARGV[0] ); my $tdirs = int($stats{$_}{tdirs}); my $tfiles = int($stats{$_}{tfiles}); my $tlinks = int($stats{$_}{tlinks}); my $dirs = int($stats{$_}{dirs}); my $files = int($stats{$_}{files}); my $links = int($stats{$_}{links}); printf "%-40s %-10s %-10s %-10s\n", $_, "$tdirs/$dirs", "$tfiles/$f +iles", "$tlinks/$lin ks"; } exit(0); #------------------------------ Sub-routines ------------------------- +--------- sub user_files { if ( $File::Find::name =~ /\/\.snapshot/ ) { $File::Find::prune = 1; return; } ($mode, $user, $group) = (stat($File::Find::name))[2,4,5]; $cur_dir = ''; my $count = 0; for my $d ( split('/', $File::Find::dir) ) { $count++; next if ( $d eq '' ); $cur_dir .= "/$d"; last if ( $count == $depth ); } if ( -f $File::Find::name ) { $tfiles++; $stats{$cur_dir}{tfiles}++; $stats{$cur_dir}{files}++ if ( $mode & S_IXOTH ); } elsif ( -d $File::Find::name ) { $tdirs++; $stats{$cur_dir}{tdirs}++; $stats{$cur_dir}{dirs}++ if ( $mode & S_IXOTH ); } elsif ( -l $File::Find::name ) { $tlinks++; $stats{$cur_dir}{tlinks}++; $stats{$cur_dir}{links}++ if ( $mode & S_IXOTH ); } }

Again, not my code and it may make more sense to start from scratch, but the goal is to change the output from:

Directory Dirs Files Links /path/to/dir1 10/8 12/10 0/0 /path/to/dir1/sub1 7/7 5/3 0/0 /path/to/dir1/sub1/a1 20/20 50/30 0/0 to this format Directory Dirs Files Links /path/to/dir1 37/35 67/43 0/0 /path/to/dir1/sub1 27/27 55/33 0/0 /path/to/dir1/sub1/a1 20/20 50/30 0/0
I hope that makes more sense...Thanks for the comments so far...they are giving me ideas on how to approach the new requirements differently.

In reply to Hash problem (possible recursion) by venrii

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.