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

I've got this subroutine which does a permutation. It works fine. I added a data structure to the program to account for a variable number of element pairs(eg. player1, player2). All of this works fine. But in the permute sub-routine a phrase is built and printed to screen. If there is ever an example where the 2nd element of a set comes before the 1st element(ie. player2 player1), the phrase is not output.

The variable that has the data structure of element sets is '$pairs'(a hash of arrays). When I call this routine I pass it 3 args: 2 arrayrefs, and the scalar hashref. Just before the 'unless' statement, $pairs is defined and houses all the right data. Inside the branch that is executed if the condition is met, $pairs is all of a sudden empty.

sub permute { my @items = @{ $_[0] }; my @perms = @{ $_[1] }; my $pairs = $_[2]; # here its defined unless (@items) { my $phrase = join( " ", @perms ); print $pairs, "\n"; exit; # here its empty all of sudden my $res = check_ord( $phrase, $pairs ); if ( !( $res ) ) { # $ARGV[0] ? print FH $phrase. "\n" : print $phrase, "\n"; } } else { my(@newitems,@newperms,$i); foreach $i (0 .. $#items) { @newitems = @items; @newperms = @perms; unshift(@newperms, splice(@newitems, $i, 1)); permute([@newitems], [@newperms]); } } }

Any ideas?

Amel - f.k.a. - kel

Replies are listed 'Best First'.
Re: Defined or Undefined - That is the question.
by enoch (Chaplain) on Mar 13, 2001 at 20:26 UTC
     @items will never be false. (At least, if I am interpreting what "Effective Perl Programming" said.) Scalars can have the value undef or 0 or "" (false); however, an array reference will never return false... I think.

    Jeremy
      Since an array returns its length in scalar context--which is the context that unless places the array in--an array of length 0 will return 0, or false. From reading Programming Perl, I thought that unless didn't permit a branching conditional. Can someone clarify this for a Perl newbie?
        On the top of page 23 in my Camel 2, it reads

        There is no "elsunless" though. This is generally construed as a feature.
        I believe the authors meant that there is no keyword that means "elsif (not EXPR)" in the way that "unless (EXPR)" means "if (not EXPR)." You can, however, use elsif and else with unless:
        foreach $a ( 0,1,2 ) { unless ( $a ) { print "a is 0 or undef\n"; } elsif ( $a == 1 ) { print "a is 1\n"; } else { print "a equals $a\n"; } } __END__ a is 0 or undef a is 1 a equals 2
        I hope this clears things up for you.

        --sacked
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Defined or Undefined - That is the question.
by dsb (Chaplain) on Mar 13, 2001 at 20:25 UTC
    Nevermind, I got it. I missed the method call in the other branch of the conditional statement. I didn't write the original code, I just have to make it better. :) Thanks anyway.

    Amel - f.k.a. - kel