I have a requirement where I need to bucket a hash and then pass this smaller hash to an thread to perform some job.

Below is a sample code which I have written which can create an array of hashes which can be used later to pass on to each threads.

#!/usr/bin/perl # use strict; use warnings; use integer; use Data::Dumper; my %hash = ( 1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd' ); my $size = keys %hash; print "Total keys are $size\n"; my $numSplit = 2; my $diffCount = $size / $numSplit; my @keys = keys %hash; my @arrHash; my %hash2; for my $i (0 .. $diffCount - 1) { my $start = $i * $diffCount; my $end = $i * $diffCount + $diffCount - 1; my %hash1; for my $j($start .. $end) { my $key = $keys[$j]; my $value = $hash{$keys[$j]}; $hash1{$key} = $value; } print Dumper(\%hash1); push(@arrHash, \%hash1); } print Dumper(\@arrHash);

But I am looking for more efficient way to do as the total number of keys can be anywhere from 2500 to 10000.


In reply to How to bucket an Hash by techman2006

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.