Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Problem printing return value from a subroutine

by AnomalousMonk (Monsignor)
on Oct 04, 2012 at 04:07 UTC ( #997175=note: print w/ replies, xml ) Need Help??


in reply to Problem printing return value from a subroutine

Further to chromatic's reply: In scalar context, an array evaluates to the number of elements of the array. This may be where you are getting your 3 from.


Comment on Re: Problem printing return value from a subroutine
Re^2: Problem printing return value from a subroutine
by zing (Beadle) on Oct 04, 2012 at 09:29 UTC
    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';

      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

        Please help me complete it

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997175]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2014-07-29 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls