in reply to constructing a tree from csv file
#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: constructing a tree from csv file
by zing (Beadle) on Nov 01, 2013 at 10:21 UTC | |
by choroba (Cardinal) on Nov 01, 2013 at 10:37 UTC | |
by zing (Beadle) on Nov 01, 2013 at 11:45 UTC | |
by zing (Beadle) on Nov 01, 2013 at 15:10 UTC | |
by choroba (Cardinal) on Nov 01, 2013 at 16:46 UTC | |
by zing (Beadle) on Nov 11, 2013 at 03:53 UTC | |
|