in reply to constructing a tree from csv file

I'd use a hash to keep the structure:
#!/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
    Dear choroba,

    Suppose P.P belongs to group 'A', P.P.E belongs to group 'B',,So Id like to have their group names output against them also.

    M143: M143<-M10<-Q = P.P, A M420: M420<-M143<-M10<-Q = P.P.E, B M407: M407<-M143<-M10<-Q = P.P.PL, C M10: M10<-Q = P, D M421: M421<-M143<-M10<-Q = P.P.E, B M406: M406<-M143<-M10<-Q = P.P.PL, C
    Of course number of groups will be hardly 4 or 5. How can I do it ?
      Create a hash that maps the method strings to groups.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thanks a lot pal !!!!!
        Dear choroba, I have not been able to find any tutorial for what you just wrote. Could you please help me on this. I though I'd try it myself but I ahve not been able to even start with it. Please if you could help me in any way !!!! thanks