I'd:

use strict; use warnings; use Data::Dump::Streamer; my @class = (40, 40, 40, 183, 183, 259, 244, 503); my @qty = (260, 234, 211, 301, 401, 321, 210, 451); my %bin = map {$_ => [0, 0]} (0, 40, 183, 503, 442, 259, 244); for my $i (0 .. $#class) { my $hkey = $class[$i]; @{$bin{$hkey}} = ($bin{$hkey}[0]+1, $bin{$hkey}[1]+ $qty[$i]); } Dump (\%bin);

Note the use of a Perl for loop in place of the C loop and an array assignment in the for loop. Oh, and keep the scope of variables as small as possible ($hkey is declared inside the loop).


True laziness is hard work

In reply to Re: Better assignment to Hash of Arrays by GrandFather
in thread Better assignment to Hash of Arrays by journey

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.