If there's a hash where one of the values is an array, what's the shortest way to split into an array-of-hashes (AoH) for each array value that keeps all the hash keys and values, except for replacing the original array with individual values?
The following illustrates the functional goal, but I feel like there's some map or similar magic that could do this as a one-liner.
use strict;
use warnings;
use Data::Dumper;
my @result;
my %hash = (
a => "string",
b => [ "13", "14", "15" ],
c => "another string",
);
foreach my $b (@{$hash{'b'}}) {
push @result, { %hash };
$result[-1]{'b'} = $b;
}
print Dumper(\@result);
Desired outcome:
$VAR1 = [
{
'b' => '13',
'a' => 'string',
'c' => 'another string'
},
{
'c' => 'another string',
'a' => 'string',
'b' => '14'
},
{
'b' => '15',
'c' => 'another string',
'a' => 'string'
}
];