P is for Practical PerlMonks

### Re^2: Problem printing return value from a subroutine

 on Oct 04, 2012 at 09:29 UTC ( #997210=note: print w/replies, xml ) Need Help??

Ok so I changed the last lines of the code
my (@qw)=induced(@{ \$subgraphs[\$d-1] }); print Dumper @qw; }
But still incorrect output
component 2 = e d \$VAR1 = 'b'; \$VAR2 = 'c'; \$VAR3 = 'a'; component 1 = c a b b c a \$VAR1 = 'b'; \$VAR2 = 'c'; \$VAR3 = 'a';

Replies are listed 'Best First'.
Re^3: Problem printing return value from a subroutine
by AnomalousMonk (Chancellor) on Oct 04, 2012 at 11:15 UTC

I must admit I haven't the foggiest notion what your code is trying to accomplish. However, I have never felt that mere ignorance should deter one from offering advice, so...

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); } }}

The quoted subroutine has two odd, nested for-loops. The outer loop

for my \$QT (\@z ){ ... undef @Pie{@\$QT}; ... print "@\$triplet\n" if keys(%Pie) <= ( @\$QT - @\$triplet ) ; ... }

uses the expression  \@z to create a single item loop list consisting of a reference to the  @z array. The loop will iterate once over this single reference, aliasing (or 'topicalizing', which I believe is the more apt term) its value to the  \$QT scalar. The  \$QT reference is used a couple of times, in each case being de-referenced to an array prior to use. So, what's the point? Why not just use the  @z array directly and forget about all the indirection and one-pass looping?

The inner for-loop

for my \$triplet ( @trip ){ my %Pie; undef @Pie{@\$QT}; delete @Pie{ @\$triplet }; print "@\$triplet\n" if keys(%Pie) <= ( @\$QT - @\$triplet ) ; return (@\$triplet); }

iterates over the global, spooky-action-at-a-distance-prone  @trip array, which seems as if it may have multiple elements. However, since the statement body of the loop ends with the
return (@\$triplet);
statement, and assuming the  @trip array is not empty to begin with, the loop can only iterate once, another one-shot for-loop. Why not just something like
my \$triplet = \$trip[0];
with the rest of the loop body retained (except for using the  @z array directly rather than via a  \$QT array reference de-reference)?

So we end up with something like (again, I have no idea what this is supposed to do):

sub induced { my (@z) = @_; my \$triplet = \$trip[0]; my %Pie; undef @Pie{ @z }; delete @Pie{ @\$triplet }; print "@\$triplet\n" if keys(%Pie) <= (@z - @\$triplet); return @\$triplet; }

Minimizing the cruft and wasted motion may allow you to see more clearly the root causes of your problems. Anyway, that's my USD0.02. I hope it helps.

Dear AM, My problem has been forked off from this thread

http://www.perlmonks.org/?node_id=997085

What Im stuck with is this :: Suppose If component=(a,b,c,d); Then I want to find the set of triplets induced by these 4 vertices. For example for above four points the induced triplets(from input data) should be:
b c a a c d
Whereas for component=(d,e,a) there isn't any triplet in the data. Similarly for component=(b,e,d) there is a triplet (d e b) in the data(the last one). The first two posts there would give you an idea about what Im trying to achieve. Thanks a ton.
__DATA__ b c a a c d d e b
Hi AM, I changed the code as per your suggestion
sub induced { my (@z) = @_; my \$triplet = \$trip[0]; my %Pie; undef @Pie{ @z }; delete @Pie{ @\$triplet }; print "@\$triplet\n" if keys(%Pie) <= (@z - @\$triplet); return @\$triplet; }
But the output is still far from wanted
component 2 = e d \$VAR1 = 'b'; \$VAR2 = 'c'; \$VAR3 = 'a'; component 1 = c a b b c a \$VAR1 = 'b'; \$VAR2 = 'c'; \$VAR3 = 'a';

Let me explain you what is wanted.Consider for example component 2. It has two vertices e,d. Now I want to see if any of the rows from "DATA" is a subset of these points.Now since each of the row of "DATA" has 3 vertices/points,therefore clearly for component two there isnt any induced line from DATA.Therefore there should be anything to print for the subroutine "induced" for this case.

But for component 1,which has vertices as = c,a,b; the first line of "DATA" which is "b c a" gets induced.

Similarly if component=a,b,c,d ; then first two rows of DATA get induced ,as they are both the subset of the component.

Hi AM, I have posted a problem similar to the one you people have been helping me with at this link http://www.perlmonks.org/?node_id=997708

Create A New User
Node Status?
node history
Node Type: note [id://997210]
help
Chatterbox?
 [Discipulus]: I havent took thine book, search it thyself.. sound very good and cool! i definitevely use Shakespeare's english Discipulus is lazily looking his automated migration in backgound and has lot of free tiime..

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2017-11-23 11:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (333 votes). Check out past polls.

Notices?