newbio has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; my $a=['a','p','q','r']; # a is the node name and p,q,r are the values + this node can assume. Likewise for other nodes. my $b=['b','t','f']; my $c=['c','nn','ny','yn','yy']; my $d=['d','abv','avb','vab']; my $values=[$b,$a,$c,$d]; my @nodes=('a','a','a','b','c','d'); foreach my $i (@nodes) { my $nodeparents=[parentchildrelationship($i)]; print "$i $nodeparents $values\n"; my $hash1=cpt($i,$nodeparents,$values); foreach my $i (keys %{$hash1}) { print "$i=>${$hash1}{$i}\n"; #print "$i\n"; } } sub cpt { my $node=$_[0]; my @parents=@{$_[1]}; my @nodevalues=@{$_[2]}; my %hash=(); my @nodeindex=(); my @temparray=(); my $string=""; my @temp=(); my $s; foreach my $i (@parents) { for (my $j=0;$j<=$#nodevalues;$j++) { if ($i eq ${$nodevalues[$j]}[0]) { push (@nodeindex, $j); } } } foreach my $i (reverse @nodeindex) { push (@temparray, $nodevalues[$i]); } for (my $i=0;$i<=$#temparray;$i++) { $string="$string"."${$temparray[$i]}[0] "; shift @{$temparray[$i]}; $temp[$i]=join (",",@{$temparray[$i]}); } $hash{$string}=1; #fill %hash with the header $s = join "\\ ", map "{$_}", @temp; #print "$s\n"; $hash{$_}=1 for glob $s; return {%hash}; $node=""; @parents=(); @nodevalues=(); %hash=(); @nodeindex=(); @temparray=(); $string=""; @temp=(); $s=""; } sub parentchildrelationship { my $node=$_[0]; #or use "shift" my %parentchild=(); my @nodeset=(); %parentchild=('b'=>['none'], 'a'=>['b','d'], 'd'=>['b'], 'c'=>['b' +]); #This is the structure of my graph, for example node 'b' has no p +arent; node 'a' has parents in 'b','d'; and node d and c have both b +as parent. push (@nodeset, $node, @{$parentchild{$node}}); return @nodeset; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tree structure problem
by kyle (Abbot) on May 08, 2007 at 17:33 UTC | |
by newbio (Beadle) on May 08, 2007 at 17:50 UTC |