use strict;
use Data::Dumper;
my %deep;
{
my %flat;
while () {
chomp;
my ( $child, $parent ) = /^(.*?):(.*?)$/;
# if the parent is new then it's possibly at the root of the structure
# like 'a' is
unless ( exists $flat{$parent} ) {
$flat{parent} = $deep{parent} = {};
}
# find the parent, and mark this node as a child of it
$flat{$parent}{$child} = ($flat{child} ||= {})
# since $child is a child of $parent it can't be at the root, so delete it if it's there
delete $deep{$child};
};
}