Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Can If-then conditions do ($a = $b = $c) type expressions?

by beamsack (Scribe)
on Apr 28, 2002 at 00:08 UTC ( [id://162578]=note: print w/replies, xml ) Need Help??


in reply to Can If-then conditions do ($a = $b = $c) type expressions?

A benefit of having the && operator is that we can optimize by choosing which of the two operands to test for equality first. If (a==b) is likely to fail we would make it the first test. This allows all kinds of oppurtunities to be clever. btw, if you want to see a language with limited control structures and the like - try Python - egads!!
  • Comment on Re: Can If-then conditions do ($a = $b = $c) type expressions?

Replies are listed 'Best First'.
Re: Re: Can If-then conditions do ($a = $b = $c) type expressions?
by snafu (Chaplain) on Apr 29, 2002 at 18:15 UTC
    I'm thinking that your optimization would simply be done using '()'s to test:
    if ( ($var1 == $var2) == $var3 )
    Would be the same as:
    if ( $var1 == $var2 && $var1 == $var3 ) {
    Wouldn't it?

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

      if ( ($var1 == $var2) == $var3 ) Would be the same as: if ( $var1 == $var2 && $var1 == $var3 ) { Wouldn't it?

      It wouldn't. You can think of "==" as if it were a normal function (and in Perl 6 it will be), that returns true or false. (both are represented internally by something special, and they are both numeric and string. In string context, true is "1" and false is "", in numeric context true is 1 and false is 0)
      So, let's read it as:

      if ( ==( ==($var1, $var2), $var3 ) ) {
      The first == returns a boolean value, which is put into numerical context because of the other ==. That one compares $var3 to the value returned (0 or 1), so the entire expression is true if:
      • $var1 and $var2 are equal and $var3 is 1
      • ... or ...
      • $var1 and $var2 are not equal and $var3 is 0

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        Right.

        I was meaning more in the hypothetical sense. I realize as Perl is now, these expressions wouldn't work. However, hypothetically or mathematically the expression (($var1 == $var2) == $var3) would be the same as Perl's ( $var1 == $var2 && $var1 == $var3 ), right? In other words, the precedence is the same, isn't it?

        _ _ _ _ _ _ _ _ _ _
        - Jim
        Insert clever comment here...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found