in reply to Re^4: constructing a tree from csv file
in thread constructing a tree from csv file

Read perldata - Perl data types to learn on how to use hashes. See below on how I would implement it.
#!/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,
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^6: constructing a tree from csv file
by zing (Beadle) on Nov 11, 2013 at 03:53 UTC
    Thanks but Im getting this error :-
    Use of uninitialized value in print at final.pl line 35, <DATA> line 7 +. , ,3: M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 35, <DATA> line 7 +. , ,0: M420<-M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 35, <DATA> line 7 +. , L,: M407<-M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 35, <DATA> line 7 +. , 0: M10<-Q = P, Use of uninitialized value in print at final.pl line 35, <DATA> line 7 +. , ,1: M421<-M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 35, <DATA> line 7 +. , L,: M406<-M143<-M10<-Q = P,
      Did you use the [download] link to get the code? Tab characters are used in the DATA section, plain spaces would break the programme.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Ohh yeah. BTW I must say, I leant a lot by simply reading through your code. Thanks it was lucidly written.
        Choroba I did it but again the same error-
        Use of uninitialized value in print at final.pl line 71, <DATA> line 7 +. , ,3: M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 71, <DATA> line 7 +. , ,0: M420<-M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 71, <DATA> line 7 +. , L,: M407<-M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 71, <DATA> line 7 +. , 0: M10<-Q = P, Use of uninitialized value in print at final.pl line 71, <DATA> line 7 +. , ,1: M421<-M143<-M10<-Q = P, Use of uninitialized value in print at final.pl line 71, <DATA> line 7 +. , L,: M406<-M143<-M10<-Q = P,
        LINE 71 being   print " = ", $path_string, ', ', $group{$path_string};
        Choroba i modified your code for my requirements and now %group hash has 40 keys, and __DATA__ has 345 lines. The problem is that the code hangs and gets killed after sometime without any output. CPU usage going to 100%,system hang. Whereas if I reduce the __DATA__ lines to 300 lines only, then it runs . Why is it so. My system has 64Gigs ram, and hexacore.
        Choroba the reason for uninitliazed error that Im getting is because my group hash doesnt have relations which are more than length 3. As you can see below. E.P.E.E is 4 relations. But thats alright as I dont want to have relations which are more than three. But could this also be the reaso n behind the program getting hanged when run on __DATA__ more than 300 lines:-
        Use of uninitialized value in print at checking.pl line 71, <DATA> lin +e 338. M61: M61<-M7<-M28<-M6<-Q = E.P.E.E, Use of uninitialized value in print at checking.pl line 71, <DATA> lin +e 338. M196: M196<-M53<-M7<-M28<-M6<-Q = E.P.E.Pl.E,