Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

flip flop operator and if statement

by iThunder (Beadle)
on Sep 20, 2014 at 02:32 UTC ( [id://1101323]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

Can someone please help that how value of p is coming out as 1 through 17.

My understanding is p should be equal to $. (line number) and it should be printed from 3 to 19 rather 1 to 17.

Thanks in advance!

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

When i run the same code on a file that has fifteen lines, it returns following

p is 1

p is 2

p is 3

p is 4

p is 5

p is 6

p is 7

p is 8

p is 9

p is 10

p is 11

p is 12

p is 13

p is 14

p is 15

p is 16

p is 17

Also what is difference when i have $p=3..1 vs ($p=3)..1 in if statement above

Replies are listed 'Best First'.
Re: flip flop operator and if statement
by GrandFather (Saint) on Sep 20, 2014 at 03:04 UTC

    Since you can't be bothered to format your node so it's readable I can't be bothered reading it, so I'll just point you at Flipin good, or a total flop? and hope that sheds some light.

    Perl is the programming world's equivalent of English
Re: flip flop operator and if statement
by Athanasius (Archbishop) on Sep 20, 2014 at 03:30 UTC

    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,

      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,

        What about them?

        Avoid it also?

Re: flip flop operator and if statement
by Anonymous Monk on Sep 20, 2014 at 12:44 UTC

    What are you actually trying to accomplish with the range / flip-flop operator? The documentation gives plenty examples of the proper use.

    You've given a piece of code and some output, but aren't completely describing what you expect the behavior and output to be. Please see How do I post a question effectively?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1101323]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-23 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found