My customer now wants to see ... the total number of directories ..., not just ...

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):

#!/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}, $_; }
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.

(updated code to remove a redundant use of m{})


In reply to Re: Hash problem (possible recursion) by graff
in thread 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.