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

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?

  • Comment on Re^4: flip flop operator and if statement

Replies are listed 'Best First'.
Re^5: flip flop operator and if statement
by Athanasius (Archbishop) on Sep 20, 2014 at 08:07 UTC

    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.
Re^5: flip flop operator and if statement
by LanX (Saint) on Sep 20, 2014 at 08:01 UTC
    > 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

        In this code you are obviously always assigning 3.

        What's the problem?

        Cheers Rolf

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