BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

#! perl -w use strict; local ($,=" - ", $\="\n"); use Quantum::Superpositions; sub min { eigenstates( any(@_) <= all(@_) ) } sub max { eigenstates( any(@_) >= all(@_) ) } my @array = qw/ 10 15 20 10 7 4 /; my $t1 = all(); print $t1; my $t2 = max(@array); print $t2; print max(@array), ($t1 = max(@array)), $t2; __DATA__ # Ouput C:\test>180278 all() 1 20 - 1 - 1

Can someone explain to me why the max function works when called in the final print statement, but fails in every attempt I make to assign its return to a variable?

I thought this might be to do with the variable needing to be blessed, hence the seemingly dumb attempt to ensure this in when I declare $t1.

sub max() appears to be doing the right thing (as you'd expect as it is copied from the Q::SP pod, but it's not so useful if I cannot assign it to anything.

Is this a "context" problem?

Thanks.

Edit by dws to expand title

Replies are listed 'Best First'.
Re: Problem using Q::SP's
by Joost (Canon) on Jul 10, 2002 at 15:09 UTC
    It seems to be working - i mean, it assigns '20' - haven't really tried to understand it at all :-) - when you assing in list context:
    #! perl -w use strict; local ($,=" - ", $\="\n"); use Quantum::Superpositions; sub min { eigenstates( any(@_) <= all(@_) ) } sub max { eigenstates( any(@_) >= all(@_) ) } my @array = qw/ 10 15 20 10 7 4 /; my ($t1) = all(); print ($t1); my ($t2) = max(@array); print $t2; print max(@array), (($t1) = max(@array)), $t2; __DATA__ this results in: all() 20 20 - 20 - 20
    -- Joost downtime n. The period during which a system is error-free and immune from user input.

      Thanks Joost. ({grin} at RMGir)

      I had a 'feeling' it was a context problem as noted. I just didn't know what to do about.

      So, spelling out what is going on here (just incase I'm not the only DSOB that reads this):

      With my $var = all(); when all() checks wantarray() to determine its calling context, it gets SCALER and therefore returns the count of the states that matched the condition.

      With my ($var)  = all();, wantarray() gives ARRAY so all() (or probably eigenstates()) returns the list (in this case containing just one value) that matched the condition.

      I know I'll be (rightfully and usefully) corrected if this is wrong:)

        Close, BrowserUK.

        I don't have the Q::SP source handy, but what's probably happening is that all() always returns an array, and then it's your assignment that determines whether that array is evaluated in scalar or list contexts.

        In a scalar context, an array always returns the count of the elements, while in a list context, an element-by-element assignment is done, so

        my ($x)=all();
        captures the first value of all()'s result in $x.
        --
        Mike
Re: Problem using Q::SP's
by RMGir (Prior) on Jul 10, 2002 at 15:10 UTC
    Maybe assigning the variable lets the cat out of the box?

    :)
    --
    Mike

    PS: Sorry, I just couldn't resist...