in reply to Re^2: flip flop operator and if statement
in thread flip flop operator and if statement

Hello again iThunder,

if(($p=3)..5)

This assigns 3 (which is non-zero and therefore true) to $p on each loop iteration. So the left-hand side of the flip-flop operator never becomes false, and the if block is always executed.

if (($c=1 and $p=3)..5)

Again, on each loop iteration this assigns 1 to $c and 3 to $p, then ands them together. And since true and true always evaluates to true, the if block is always executed.

This is beginning to look like an XY Problem. What are you really trying to do with the flip-flop operator?

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: flip flop operator and if statement
by iThunder (Beadle) on Sep 20, 2014 at 07:32 UTC

    but then what should be the value of $p.

    " Perl, as a convenience, returns “true” in the form of a sequence number."

    shouldnt the value of p in every iteration increment by one?

      Yes, if $p were being assigned the return value of the flip-flop operator, as in the OP, then it would indeed be incremented by one on each iteration in which the flip-flop operator evaluates to true. But by using parentheses in:

      if(($p=3)..5)

      you ensure that 3 is assigned to $p before the flip-flop operator is evaluated. Then the value of $p (now 3) is used in the evaluation of the flip-flop operator; and since 3 is “true” in Perl, the operator returns a sequence number (which is also true). That sequence number is not assigned to any variable, it’s just used to determine the outcome of the if statement.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Thanks Athanasius.
      > shouldnt the value of p in every iteration increment by one?

      In your OP you already showed that it does.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        it prints 3 for every iteration i.e.

        #!/usr/bin/perl ##use warnings; while(<>){ if(($p=3)..5) { print "p is $p\n"; } }

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3

        p is 3