I don't get this code?

You take the keys in turn and break them into their constituant parts.

while(my($key,$val) = each(%ret)) { my @parts = split(/\./, $key); my $id = ''; foreach my $add (@parts) {

Then you iterate over those parts and concatenate them back together, piece by piece, into $id.

$id .= $add;

And then, if $id (so far) doesn't match $key, you skip to the next iteration

if($id eq $key) { next; }

Which means you'll never reach this code, until you've completely re-built $id to match $key.

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 .= '.'; } }

Which, unless I'm missing something (quite possible), there is no need for split or the for loop as this would do the same thing:

while(my($key,$val) = each(%ret)) { $id = $key; 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'; } ## Though this doesn't do much? $ret{$id}{'count'} += $ret{$key}{'count'}; }

What did I miss?

BTW. It won't help much towards your performance goal, but you can save a bit of memory by changing:

my @keys = sort keys %cats; my $cnt = scalar @keys; ... @keys = keys %ids; $cnt = scalar @keys; ... @keys = keys %ret; $cnt = scalar @keys;

to

my $cnt = keys %cat; ... $cnt = keys %ids; ... $cnt = keys %ret;

which does the same thing without copying all the keys to an array or sorting one set of them for no reason. Actually, if your low on memory, avoiding that sort and the (re-)allocation of those arrays might help your performance.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re^3: Architectural question... by BrowserUk
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.