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