Re: Can If-then conditions do ($a = $b = $c) type expressions?
by premchai21 (Curate) on Apr 25, 2002 at 16:46 UTC
|
if (not grep {$a!=$_} ($b, $c)) { ... }
Note that if you want a string comparison, replace != with ne.
| [reply] [Watch: Dir/Any] [d/l] |
|
# 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. | [reply] [Watch: Dir/Any] [d/l] |
|
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.
| [reply] [Watch: Dir/Any] |
Re: Can If-then conditions do ($a = $b = $c) type expressions?
by Fletch (Bishop) 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).
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
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)
Amel | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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 ;Þ
| [reply] [Watch: Dir/Any] |
|
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)) {...}
;-) | [reply] [Watch: Dir/Any] [d/l] |
|
just by observing this, i have been changed :)
~Particle *accelerates*
| [reply] [Watch: Dir/Any] |
Re: Can If-then conditions do ($a = $b = $c) type expressions?
by CharlesClarkson (Curate) on Apr 26, 2002 at 05:25 UTC
|
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";
}
HTH,
Charles K. Clarkson
Clarkson Energy Homes, Inc.
| [reply] [Watch: Dir/Any] [d/l] [select] |
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
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.
=cut
--Brent Dax
There is no sig. | [reply] [Watch: Dir/Any] |
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... | [reply] [Watch: Dir/Any] |
|
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.
humbly, novitiate
"...goodnight you princes of main(e)"  --The Cider House Rules
| [reply] [Watch: Dir/Any] |
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!! | [reply] [Watch: Dir/Any] |
|
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... | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] |
|