Let me try to elaborate.. This code:
foreach my $id (0..$x) { my $cat = $ids{$id}; if(!$ret{$ids{$id}}{'count'}) { $ret{$ids{$id}}{'count'} = 0; $ret{$ids{$id}}{'head_count'} = 'head_count'; $ret{$ids{$id}}{'cat_count'} = 'cat_count'; $ret{$ids{$id}}{'subcat_count'} = 'subcat_count'; } $ret{$ids{$id}}{'count'}++; }
.. builds a hash where the key is something like "a.b.c.d". .. The problem is the total for a.b.c needs to be a sum of all the categories beneath it a.b.c.* and thus the top level category is the sum of "a.*".

This "summation" is handled by the next snippet:
while(my($key,$val) = each(%ret)) { my @parts = split(/\./, $key); my $id = ''; foreach my $add (@parts) { $id .= $add; if($id eq $key) { next; } if(!$ret{$id}{'count'}) { $ret{$id}{'count'} = 0; $ret{$id}{'head_count'} = 'head_count'; $ret{$id}{'cat_count'} = 'cat_count'; $ret{$id}{'subcat_count'} = 'subcat_count'; } $ret{$id}{'count'} += $ret{$key}{'count'}; $id .= '.'; } }

In the example where $key = 'a.b.c.d' each loop of the foreach loop would look something like:
Loop #1: a Loop #2: a.b Loop #3: a.b.c Loop #4: a.b.c.d. (skipped)

I hope this clarifies!

- dEvNuL

In reply to Re^4: Architectural question... by devnul
in thread Architectural question... by devnul

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.