I have the following code to try and split a hash into say 2 other hashes, trying to maintain an equal distribution, but it skips one key. What is the best way to split a hash by key into N multiple hashes?
#!/usr/bin/perl use strict; use warnings; $|++; use Data::Dumper; my $numOfHashes = 2; my $numOfHashesO = 2; my $worker; my $hashref = { '6712' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6712' }, '6582' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6582' }, '6282' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6282' }, '6462' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6462' }, '16712' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6712' }, '26582' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6582' }, '36282' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6282' }, '46462' => { 'IP_ADDRESS' => '1.2.3.44', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6462' }, '96182' => { 'IP_ADDRESS' => '9.0.0.0', 'IPV6' => '0', 'IP_ADDRESS_BIN' => '000000000000000000000000000 +000000000000', 'CSX_ID' => '6182' } }; my $keys = keys %{$hashref}; print "Original Hash Keys: $keys\n"; foreach my $key ( keys %{$hashref} ) { if ( $numOfHashes == 0 ) { $numOfHashes = $numOfHashesO; $worker->{$numOfHashes}->{WORK}->{$key} = $hashref->{$ +key}; } $worker->{$numOfHashes}->{WORK}->{$key} = $hashref->{$ +key}; $numOfHashes--; } foreach my $destKey ( keys %{$worker} ) { my $destKeyCount = keys %{$worker->{$numOfHashes}->{WORK}} +; print "$destKey: $destKeyCount\n" }

In reply to How can I split a hash into other N hashes by ataX

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.