develooper Front page | perl.perl5.porters | Postings from April 2000

chained comparisons

Thread Next
Jeff Pinyan
April 19, 2000 14:09
chained comparisons
Message ID:
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           
PerlMonth - An Online Perl Magazine  
The Perl Archive - Articles, Forums, etc.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About