I have the following hash
my %input_hash = (
'test1' => '100',
'test2' => '200',
'test3' => '300',
'test4' => '400',
'test5' => '500'
);
What I need is to build a hash of hash from the above hash. I need to put the first 2 of the above key value pair into a key of the hash of hash. Better explained with this example.
Desired output:
my %expected_hash = (
1 => {
'test1' => '100',
'test2' => '200',
},
2 => {
'test3' => '300',
'test4' => '400',
},
3 => {
'test5' => '500'
},
);
I would like the split to be dynamic. Example,if i need to split by 3, the desired output should be
my %expected_hash = (
1 => {
'test1' => '100',
'test2' => '200',
'test3' => '300',
},
2 => {
'test4' => '400',
'test5' => '500'
},
);
Here’s a solution using an index array built from number of keys in
%input_hashand desired size set in$chunk_size:Output:
Of course, as TLP mentioned, you have to sort
%input_hashto achieve this.