Anonymous Monk's code can be slightly beautified:
use strict;
use Data::Dumper;
my %deep;
{
my %flat;
while (<DATA>) {
chomp;
my ( $child, $parent ) = /^(.*?):(.*?)$/;
# if the parent is new then it's possibly at the root of the struc
+ture
# 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 d
+elete it if it's there
delete $deep{$child};
};
}