#!/usr/bin/perl use warnings; use strict; my %group = ('P.P' => 'A', 'P.P.E' => 'B', 'P.P.PL' => 'C', 'P' => 'D', ); <DATA>; # Skip header. my %tree; while (<DATA>) { 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,
In reply to Re^5: constructing a tree from csv file
by choroba
in thread constructing a tree from csv file
by zing
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |