You could use the sum function from List::Util:
use List::Util qw/sum/; $header{"Number of Drills"} = sum map $_->count, map { values %{ $_->tool } } @drill_arr;
I haven't benchmarked this so don't know how it compares to yours. I'd guess it'd be faster since yours has to build up a code ref then get rid of it, etc. And sum is implemented in C, so it's fast.

Update: okay, benchmarked them:

timethese(-6, { 'sum' => sub { my $n = sum map $_->{count}, map { values %{ $_->{tool} } } @drill_arr; }, 'sub' => sub { my $n = &{ sub { my $ret; foreach my $l ( @drill_arr ) { foreach ( values %{$l->{tool}} ) { $ret += $_->{count}; } } return $ret; } }; }, });
Results:
Benchmark: running sub, sum, each for at least 6 CPU seconds... sub: 7 wallclock secs ( 6.00 usr + 0.00 sys = 6.00 CPU) @ 15 +883.17/s (n=95299) sum: 6 wallclock secs ( 6.02 usr + 0.00 sys = 6.02 CPU) @ 40 +993.85/s (n=246783)
So using sum is lots faster, apparently.

Note however that I'm not using method calls; I just set up a dummy array holding hashes of hashes that I'm using.


In reply to Re: Am I developing poor style? by btrott
in thread Am I developing poor style? by el-moe

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.