Prev

Perl Notes

Next

Chapter 6.2 Comparison Operators

Comparing Integers

OperatorDescriptionExample
==equal toif ($age == 36)
!=not equal toif ($age != 36)
>greater thanif ($age > 36)
<less thanif ($age < 36)
>=greater than or equal toif ($age >= 36)
<=less than or equal toif ($age <= 36)

Comparing Strings

OperatorDescriptionExample
eqequal toif ($name eq "Ralph")
nenot equal toif ($name ne "Ralph")
gtgreater thanif ($name gt "Ralph")
ltless thanif ($name lt "Ralph")
gegreater than or equal toif ($name ge "Ralph")
leless than or equal toif ($name le "Ralph")

Compound Comparison

OperatorDescriptionExample
&&logical and operatorif (($name eq "Ralph")&&($age > 45))
With two comparisons and the logical and operator, the entire condition
is true if and only if the value of both comparisons is true. In other
words, the person in question must be named Ralph and must be older than 45.
||logical or operatorif (($name eq "Ralph")||($age > 45))
If you use the logical or operator, only one of the comparisons needs
to return true for the condition to be true. In this case, persons older
than 45, regardless of their name, or people named Ralph, regardless of
their age, would match.


Prev Home Next