in reply to constructing a tree from csv file

Ahem, this isn't going to win style points (I think), but here goes:
#! /usr/bin/perl -l my %T = map {$_->[0] => $_} map {[split /,\s*/]} <DATA>; sub xz4 { ["$_[0]<-$_[2]", "$_[3].$_[1]"] } sub xln { my @r = @{@T{@_} // return (@_, '')}; @{ $r[3] //= xz4 @r[0,2], xln($r[1]) }; } print grep {s/\./= /} join " ", xln($_) for keys %T; __DATA__ M10, Q, P, M143, M10, P, M406, M143, PL, M407, M143, PL, M420, M143, E, M421, M143, E,

Replies are listed 'Best First'.
Re^2: constructing a tree from csv file
by zing (Beadle) on Nov 21, 2013 at 07:27 UTC
    oiskuu if I run your code on this data:-
    __DATA__ M7, Q, P, M7, M28, E, M28, M6, E, M6, Q, Pl,
    I get this
    M7<-M28<-M6<-Q = Pl.E.E M6<-Q = Pl M28<-M6<-Q = Pl.E
    Whereas I expect
    M7<-Q = P M28<-M6<-Q = Pl.E M6<-Q = Pl
    That is I expect only the shortest path from child to parent if it is there.
      It appears your problem has substantially altered. Please post a new question with all specifics.
      Are the relations fixed to those three? Do they have a cost metric? If multiple shortest paths exist?
        Created a seperate node as per request : http://www.perlmonks.org/?node_id=1063871 Please help