#!/usr/bin/perl use warnings; use strict; my %group = ('P.P' => 'A', 'P.P.E' => 'B', 'P.P.PL' => 'C', 'P' => 'D', ); ; # Skip header. my %tree; while () { chomp; my ($child, $parent, $relation) = split /,\t/; $relation =~ s/,$//; $tree{$child}{parent} = [$parent, $relation]; } for my $node (keys %tree) { my @path = $node; my @relations = ($tree{$node}{parent}[1]); my $parent = $node; while ($parent = $tree{$parent}{parent}[0]) { push @path, $parent; push @relations, $tree{$parent}{parent}[1]; } my $path_string = join '.', reverse grep defined, @relations; print "$node:\t"; print join '<-', @path; print " = ", $path_string, ', ', $group{$path_string}; print "\n"; } __DATA__ child, Parent, relation M10, Q, P, M143, M10, P, M406, M143, PL, M407, M143, PL, M420, M143, E, M421, M143, E,