#!/usr/bin/perl use warnings; use strict; <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]; } print "$node:\t"; print join '<-', @path; print " = ", join '.', reverse grep defined, @relations; print "\n"; } __DATA__ child, Parent, relation M10, Q, P, M143, M10, P, M406, M143, PL, M407, M143, PL, M420, M143, E, M421, M143, E,
Output:
M143: M143<-M10<-Q = P.P M420: M420<-M143<-M10<-Q = P.P.E M407: M407<-M143<-M10<-Q = P.P.PL M10: M10<-Q = P M421: M421<-M143<-M10<-Q = P.P.E M406: M406<-M143<-M10<-Q = P.P.PL
In reply to Re: constructing a tree from csv file
by choroba
in thread constructing a tree from csv file
by zing
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |