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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |