Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by premchai21 (Curate)
on Apr 25, 2002 at 16:46 UTC ( #162028=note: print w/replies, xml ) Need Help??

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

How about:

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

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

Replies are listed 'Best First'.
Re: Re: Can If-then conditions do ($a = $b = $c) type expressions?
by admiralh (Sexton) on Apr 26, 2002 at 04:43 UTC
    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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2023-02-04 22:12 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.