Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: Architectural question...

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

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

I re-pasted it, please let me know if it still isn't working.....

... If you cut and paste the code from here you may need to make 2 edits on line 33 and 52 because those lines wrap and have + symbols on them...

- dEvNuL

Replies are listed 'Best First'.
Re^3: Architectural question...
by BrowserUk (Pope) on Jul 05, 2005 at 10:58 UTC

    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;


    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.
      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://472396]
[marto]: so I have to work Saturday, which is good and bad. Bad in the sense that I have to be hear early :( Good in that I am here only as support for another client project, so I get to spend the day working on my personal 'ToDo' list, without the kids :P
[Corion]: marto: Ooof - but yes, being there as only second level support or third level support is good and if you have enough things that you can work on, it won't be a wasted (if paid) day
[marto]: I've opted for a day off later on, rather than pay. I only have to do about 45 mins work for the client, but do need to hang around
[Corion]: marto: Even better ;)
[marto]: and I'm off next week1

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-02-28 10:08 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (399 votes). Check out past polls.