Hi all, I have this code
#This program read the triplets from file named "data" into #an array of array. use strict; use warnings; use Data::Dumper; use Graph; use Graph::Subgraph; my @S; while (<>) { push @S, [ split ]; } print "-----TRIPLETS-------\n"; print Dumper \@S; #Make a copy of @S my @trip = map { [@$_] } @S; # Find the number of vertices my @L; for my $i ( 0 .. $#S ) { for my $j ( 0 .. $#{ $S[$i] } ) { push (@L,$S[$i][$j]); } } my %seen; @L = grep { ! $seen{ $_ }++ } @L; print " ----VERTICES------\n"; print Dumper \@L; # Now lets generate the G(L) # In order to generate the G(L) we'll extract first two columns of S i +nto another matrix my @GL=@S; splice(@$_, 2, 1) foreach @GL; print "----EDGE LIST TO BUILD G(L)-----\n"; print Dumper \@GL; #my %h = map { $_->[0] => $_->[1] } @S; #print Dumper(\%h); ##### CONNECTED COMPONENTS ########## my $g = Graph->new( undirected => 1 ); my @a; my @b; for (my $p = 0; $p <= 2; $p++) { $a[$p]=$S[$p][0]; } for (my $q = 0; $q <= 2; $q++) { $b[$q]=$S[$q][1]; } for (my $r = 0; $r <= 2; $r++) { $g->add_edge($a[$r], $b[$r]); } my @subgraphs = $g->connected_components; my @allgraphs; my $V = $g->vertices; print "Number of taxa=$V\n"; my $q=scalar @subgraphs; print "Number of connected components ", $q , "\n"; print "First connected component: ", @{ $subgraphs[0] }, "\n"; print "First connected component element: ", $subgraphs[0][1], "\n\n"; sub induced { my (@z)=@_; for my $QT (\@z ){ #print Dumper $QT; for my $triplet ( @trip ){ my %Pie; undef @Pie{@$QT}; delete @Pie{ @$triplet }; print "@$triplet\n" if keys(%Pie) <= ( @$QT - @$triplet ) ; return (@$triplet); } }} my @C; my $d; my $p=$#subgraphs+1; for ($d=$p; $d >=1; $d--) { print "component $d = @{ $subgraphs[$d-1] }\n"; my $qw=induced(@{ $subgraphs[$d-1] }); print "induced=$qw\n"; }
It takes in the data from data file ,the content of which is
b c a a c d d e b
---OUTPUT----
----TRIPLETS------- $VAR1 = [ [ 'b', 'c', 'a' ], [ 'a', 'c', 'd' ], [ 'd', 'e', 'b' ] ]; ----VERTICES------ $VAR1 = [ 'b', 'c', 'a', 'd', 'e' ]; ----EDGE LIST TO BUILD G(L)----- $VAR1 = [ [ 'b', 'c' ], [ 'a', 'c' ], [ 'd', 'e' ] ]; Number of taxa=5 Number of connected components 2 First connected component: cba First connected component element: b component 2 = e d induced=3 component 1 = c b a b c a induced=3
Problem is with the last 5 lines of the output ,it should have been this
component 2 = e d component 1 = c b a induced=b c a
I know the problem is there in the way the subroutine return value is saved. Please suggest me why is this happening and how to fix it.

In reply to Problem printing return value from a subroutine by zing

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.