Front page | perl.perl5.porters |
Postings from April 2000
chained comparisons
Thread Next
From:
Jeff Pinyan
Date:
April 19, 2000 14:09
Subject:
chained comparisons
Message ID:
Pine.GSO.4.21.0004191702130.28375-100000@crusoe.crusoe.net
Ok, I know Perl does not support "chained comparisons", like Python does:
if a < b == c < d:
# ...
That would have to be written as
if (($a < $b) and ($b == $c) and ($c < $d)) {
...
}
One problem standing in Perl's way is the precedence that <, <=, >, and
>= have == and !=. In Python, they have the same precedence (or they do
in the context of chain comparisons).
I am curious, though, why
$a < $b < $c
is a syntax error. Why doesn't Perl parse this as
($a < $b) < $c
? Is it because comparison operators are non-associative?
--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve japhy@pobox.com
http://www.pobox.com/~japhy/ http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http://www.perlarchive.com/
Thread Next
-
chained comparisons
by Jeff Pinyan