Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: Architectural question...

by devnul (Monk)
on Jul 05, 2005 at 17:50 UTC ( #472540=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Architectural question...
in thread Architectural question...

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


Comment on Re^4: Architectural question...
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://472540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-12-20 05:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls