techman2006 has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to bucket a Hash
by Athanasius (Archbishop) on Nov 27, 2013 at 06:56 UTC | |
by techman2006 (Beadle) on Nov 27, 2013 at 07:29 UTC | |
by Random_Walk (Prior) on Nov 27, 2013 at 09:41 UTC | |
|
Re: How to bucket an Hash
by hdb (Monsignor) on Nov 27, 2013 at 06:30 UTC | |
|
Re: How to bucket an Hash
by BrowserUk (Patriarch) on Nov 27, 2013 at 10:30 UTC | |
by techman2006 (Beadle) on Nov 27, 2013 at 16:53 UTC | |
by BrowserUk (Patriarch) on Nov 27, 2013 at 17:21 UTC | |
by techman2006 (Beadle) on Nov 27, 2013 at 17:50 UTC | |
by BrowserUk (Patriarch) on Nov 27, 2013 at 18:24 UTC |