=head2 C<< flatten LISTNAME CHILDLIST $var >> Removes one level of hierarchy and merges all keys from the current hierarchy into the elements below it: $VAR1 = { user => 'corion', pages => [ { title => 'This is page 1', url => '/pages/1', items => [ { url => '/items/1', description => 'A brand new item' }, { url => '/items/2', description => 'A brand new item' }, ], }, { title => 'This is page 2', url => '/pages/2', items => [ { url => '/items/3', description => 'A brand new item' }, { url => '/items/4', description => 'A brand new item' }, ], }, { title => 'This is page 3', url => '/pages/3', items => [] }, ], } flatten 'pages' => 'items', $VAR1 becomes $VAR1 = { user => 'corion', items => [ { title => 'This is page 1', url => '/items/1', description => 'A brand new item' }, { title => 'This is page 1', url => '/items/2', description => 'A brand new item' }, { title => 'This is page 2', url => '/items/3', description => 'A brand new item' }, { title => 'This is page 2', url => '/items/4', description => 'A brand new item' }, }, ], } =cut sub flatten($$$) { my ($key,$child,$items) = @_; if (! exists $items->{$key}) { croak "Cannot flatten '$key': The entry does not exist"; }; $items->{ $child } = [ map { my $p = $_; (exists $p->{ $child } && ref $p->{ $child } eq 'ARRAY') ? (map {; +{%$p,%$_} } @{ delete $p->{ $child }} ) : () } @{ delete $items->{ $key }} ]; $items };