sub tree_data {
my ( $old_data, $key_name, $next_group, @remaining_keys ) = @_;
my ( @new_data, $last_key, $build_box );
foreach ( @{ $old_data } ) {
if ( $last_key and ( $last_key ne $_->{ $key_name } ) ) {
push @new_data, {
$key_name => $last_key,
$next_group => ( @remaining_keys )
? tree_data( $build_box, @remaining_keys )
: $build_box,
};
undef $build_box;
}
$last_key = delete $_->{ $key_name };
push @{ $build_box }, $_;
}
push @new_data, {
$key_name => $last_key,
$next_group => ( @remaining_keys )
? tree_data( $build_box, @remaining_keys )
: $build_box,
};
return \@new_data;
}
####
my $input = [
{
'team' => 'A-Team',
'employee' => 'Face',
'work_day' => '2006-08-28',
'other_data' => '123456789',
},
{
'team' => 'A-Team',
'employee' => 'Murdock',
'work_day' => '2006-08-28',
'other_data' => '123456789',
},
{
'team' => 'Military',
'employee' => 'Decker',
'work_day' => '2006-08-28',
'other_data' => '123456789',
},
];
##
##
my $output = [
{
'team' => 'A-Team',
'employees' => [
{
'employee' => 'Face',
'work_day' => '2006-08-28',
'other_data' => '123456789',
},
{
'employee' => 'Murdock',
'work_day' => '2006-08-28',
'other_data' => '123456789',
},
],
},
{
'team' => 'Military',
'employees' => [
{
'employee' => 'Decker',
'work_day' => '2006-08-28',
'other_data' => '123456789',
},
],
},
];
##
##
# ...to generate the output example from above...
my $output = tree_data( $input, 'team', 'employees' );
# ...to go a little deeper...
my $output = tree_data(
$input, 'team', 'employees', 'employee', 'work_days'
);