Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

by snafu (Chaplain)
on Apr 25, 2002 at 16:43 UTC ( #162026=perlquestion: print w/ replies, xml ) Need Help??
snafu has asked for the wisdom of the Perl Monks concerning the following question:

So, I was curious about this and looked all over Super Search and google but I am probably forming my search query poorly.

My question is one that I have wondered about for sometime but never pursued out of lack of time. However, I am very curious now to learn how I could do this if at all.

The following code attempt fails:

perl -e ' $a = 5; $b = 5; $c = 5; if ( ($a == $b) == $c ) { print "They are the same\n"; } else { print "They are not the same\n"; } '
Obviously, I had to declare precedence so that the intperpretter wouldn't get confused as to what to compare first then next , but, this still doesn't work. I've tried various other syntactical tweaks in the condition as well but haven't been able to find one that works. I know that you could and or && this but I would like to avoid that if possible.

if ( $a == $b and $a == $c ) { print "true\n"; } else { print "not true"; }
I am thinking that this kind of operation would make it very easy to see if a number of variables all equal the same value which could be done in one condition without extra operators or repeating the lvalue more than once.

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

Comment on Can If-then conditions do ($a = $b = $c) type expressions?
Select or Download Code
Replies are listed 'Best First'.
Re: Can If-then conditions do ($a = $b = $c) type expressions?
by premchai21 (Curate) on Apr 25, 2002 at 16:46 UTC

    How about:

    if (not grep {$a!=$_} ($b, $c)) { ... }

    Note that if you want a string comparison, replace != with ne.

      I will certainly agree that this works.

      I also say this is not obvious at all, and I defy you to remember 6 months from now what in the world you were trying to do. The code

      # Test if $a, $b, and $c are equal # to the same number if ($a == $b && $a == $c) { print "Three agree\n"; } else { print "Three don't agree\n"; }
      is clear, easy to read, and understandable to anyone who would be in position to maintain your code.

      Cool code is fun, but maintaining cool code is a nightmare.

        If I'm not mistaken, it was specified in the original node that e was looking for alternatives to the method in your post. Most likely TheDamian's solution is the most readable and concise, but it uses a non-core module, which can be problematic in some situations. What I posted was just another WTDI.

        Yes, I would remember what it meant six months afterward; if there were any chance someone else would have to maintain it, I would most likely abstract it into a subroutine, or at least put a comment there (see below).

        And while in almost all cases the original method (the same as yours) is more readable, at some point the number of equalities being tested becomes unwieldy, and it becomes more efficient readability-and-writability-wise to abstract the test out into a grep.

Re: Can If-then conditions do ($a = $b = $c) type expressions?
by Fletch (Chancellor) on Apr 25, 2002 at 16:52 UTC

    $a == $b is 1, which is obviously not equal to 5. As was recommended, grep's probably the way to go. Or

    $h{$_}++ for( $a, $b, $c ); if( $h{$a} == 3 ) { ... }

    Update: Just as a clarification, that hash trick will only work for things that stringify to the same thing. So for example if $b = "5 ", it wouldn't work (even though 5 == "5 " is true).

      Ahhh....yes. It didn't occur to me that ($a == $b) actually evaluates to true or 1 vs the value that they are set to which now makes sense. Excellent! Although, I do think that this kind of operation would be a nifty one to have available. It probably would be difficult to impossible to implement though.

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

        I don't know about easy to read but it can be done in a roundabout sort of way:
        $a = 5; $b = 5; $c = 5; if ((($a == $b ) + ($a -1)) == $c) { print "yes!!!\n"; }
        or better yet:
        # given three equal variables if (($a == $b ) == ($b == $c)) { print "yes!!!\n"; } # as pointed out by Sidhekin # given three different variables if (($a == $b ) == ($b == $c)) { print "yes!!!\n"; } # still prints 'yes!!!\n'. Why? # both sides evaluate to false or 0, # and 0 == 0 evaluates to true
        Again, I'm not saying I'd use this... ;0)

        Update: Thanks to Sidhekin for pointing out my misstep. It does work with 2 same and 1 different variable. ;0)

Re: Can If-then conditions do ($a = $b = $c) type expressions?
by particle (Vicar) on Apr 25, 2002 at 18:07 UTC
    this is coming in perl 6, as well as chaining file test operators, and plenty other fancy doodads (see Exegesis 3.)

    i can't wait!

    ~Particle ;

      I can't wait!
      But while you are waiting, there are always 23rd century programming techniques to fall back on:
      use Quantum::Superpositions; if ($a == all($b,$c)) {...}
        just by observing this, i have been changed :)

        ~Particle *accelerates*

Re: Can If-then conditions do ($a = $b = $c) type expressions?
by CharlesClarkson (Curate) on Apr 26, 2002 at 05:25 UTC

    Enter Quantum::Superpositions

    use Quantum::Superpositions; my $a = my $b = my $c = 5; if ( all( $a, $b, $c) == 5) { print "They are all 5\n"; } else { print "They are not the same\n"; }

    Or more generally

    use Quantum::Superpositions; my $a = my $b = my $c = 5; if ( all($a, $b, $c) == all( $a, $b, $c) ) { print "They are the same\n"; } else { print "They are not the same\n"; }

    Update: The second idiom is better written as:

    if ( all($a, $b, $c) ) { print "They are the same\n"; } else { print "They are not the same\n"; }

    Charles K. Clarkson
    Clarkson Energy Homes, Inc.
Re: Can If-then conditions do ($a = $b = $c) type expressions?
by robot_tourist (Hermit) on Apr 26, 2002 at 07:08 UTC

    Do be careful with $a and $b as they are used for sort (see perlfunc). I haven't needed any complicated sorting routines yet, but I have seen a lot of nodes on this site where the difficulty is in using the special variables $a and $b.

    I have a wiry brain/each eye a camera. Robot Tourist, by Ten Benson

Re: Can If-then conditions do ($a = $b = $c) type expressions?
by benwall (Initiate) on Apr 26, 2002 at 04:49 UTC
    I think that the real question here is not how you do this, but why are you doing this... If you know that the "&&" operator works, then why aren't you using it!!!

    The hallmark of good code is not that is it brilliant or different than anyone elses, it is exactly the opposite. Good code is code that can be understood by the beginning programmer, and every beginning programmer is taught to use the && operator in this situation.. It is there because it works for exactly this, and it is probably way faster than using "grep" or any other mathematical thing that you come up with if(($b == $a) == $c/$a) or some such thing...!!! ARGH.. dont be different CONFORM!!!!!

    lol.. some of that last stuff is joking but really please wrote code that is easy to understand...

      Whence the notion that good code is "code that a beginner could understand". Granted, this example may lend itself to this argument; excellent code is judged on a number of criteria, but not every beginner can recognize, or make sense of,the genius of some things. Sometimes, we
      mistake simple for easy. The simplest methods/ways/equations might be the best
      99% of the time; but we usually only recognize their genius and make sense of them after much contemplation.


      "...goodnight you princes of main(e)"  --The Cider House Rules
Re: Can If-then conditions do ($a = $b = $c) type expressions?
by BrentDax (Hermit) on Apr 26, 2002 at 07:45 UTC
    Perl 6 will do that, but Perl 5 doesn't.

    At least not yet... *evil grin*

    Actually, that's a cool idea. I may have to consider putting it in.

    --Brent Dax
    There is no sig.

Re: Can If-then conditions do ($a = $b = $c) type expressions?
by beamsack (Scribe) on Apr 28, 2002 at 00:08 UTC
    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!!
      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://162026]
Approved by jlk
Front-paged by japh
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2015-11-28 12:06 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (741 votes), past polls