in reply to flip flop operator and if statement

Hello iThunder,

As GrandFather says, you should put text into paragraphs using <p> ... </p> tags, and you should enclose code and data in <code> ... </code> tags. I also moved your post from Perl Monks Discussion to Seekers of Perl Wisdom.

Regarding the flip-flop operator (viz., .. in scalar context), you are confusing line numbers with sequence numbers. Actually, the flip-flop operator evaluates to a boolean value, i.e., either true or false. When the operator evaluates to true, Perl, as a convenience, returns “true” in the form of a sequence number. Consider:

use strict; use warnings; my $p; while (<DATA>) { chomp; if ($p = 3 .. 5) { print "$_: \$. is $.: p is $p\n"; } } __DATA__ line 1 line 2 line 3 line 4 line 5 line 6 line 7

Output:

14:00 >perl 1020_SoPW.pl line 3: $. is 3: p is 1 line 4: $. is 4: p is 2 line 5: $. is 5: p is 3E0 14:00 >

Note that the string E0 is appended to the final true value: this “gives you something to search for if you want to exclude the endpoint.” — perlop#Range-Operators.

Update: Added printout of $..

Hope that helps,

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

Replies are listed 'Best First'.
Re^2: flip flop operator and if statement
by iThunder (Beadle) on Sep 20, 2014 at 06:23 UTC
    Hi Athanasius,

    Thanks for quick response. What about if the if statement is like

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

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

      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,

        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?

      What about them?

      Avoid it also?