in reply to Re^2: Problem printing return value from a subroutine
in thread Problem printing return value from a subroutine
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Problem printing return value from a subroutine
by zing (Beadle) on Oct 04, 2012 at 12:03 UTC | |
|
Re^4: Problem printing return value from a subroutine
by zing (Beadle) on Oct 04, 2012 at 15:40 UTC | |
|
Re^4: Problem printing return value from a subroutine
by zing (Beadle) on Oct 07, 2012 at 20:16 UTC |