Dear Monks,

I am trying to create a graphical structure with four nodes with names a, b, c and d. (%parentchild in the code below shows the graphical structure).
The program is giving the desired output for $nodes[0] (in the first foreach loop of the code below - output for $nodes[0] is also shown below). The output must contain all possible combinations of values of the node and its parents.
However, all subsequent iterations of this foreach loop (for $nodes[1], $nodes[2], $nodes[3], $nodes[4], and $nodes[5])
give incorrect results. I am not being able to figure out what mistake I am making.

Please guide me where the fault is.

Thanks very much.

#!/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; }

In reply to Graphical structure problem by newbio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.