Comparison operators
Comparison operators take operands of several data types, and resolve to Boolean values.
Comparisons of numbers convert the
operands to and from floating point and integer values, such that 1.0 == 1
is true. However, keep in mind that
floating point values created by division are inexact, so mathematically equal
values can be slightly unequal when turned into floating point values.
You can compare any two values with equals ==
or not equals !=
, but only strings, numbers, and data types that
require values to have a defined order can be compared with the less than or greater
than operators.
Characters are compared based on their encoding. For characters in the US ASCII range, punctuation comes before digits, digits are in the order 0, 1, 2, ... 9, and letters are in alphabetical order. For characters outside US ASCII, ordering is defined by their UTF-8 character code, which might not always place them in alphabetical order for a given locale.
== (equality)
Resolves to true
if the operands are equal. Accepts the following
data types as operands:
Numbers: Tests simple equality.
Strings: Tests whether two strings are identical, ignoring case as described in the Note, above.
Arrays and hashes: Tests whether two arrays or hashes are identical.
Booleans: Tests whether two Booleans are the same value.
Data types: Tests whether two data types would match the exact same set of values.
1 == "1"
is false, and "true" ==
true
is false.
!= (non-equality)
Resolves to false
if the operands are equal. So, $x !=
$y
is the same as !($x == $y)
. It has the same
behavior and restrictions, but opposite result, as equality ==
,
above.
< (less than)
Resolves to true
if the left operand is smaller than the right
operand. Accepts numbers, strings, and data types; both operands must be the same
type. When acting on data types, a less-than comparison is true
if
the left operand is a subset of the right operand.
> (greater than)
Resolves to true
if the left operand is larger than the right
operand. Accepts numbers, strings, and data types; both operands must be the same
type. When acting on data types, a greater-than comparison is true
if the left operand is a superset of the right operand.
<= (less than or equal to)
Resolves to true
if the left operand is smaller than or equal to the
right operand. Accepts numbers, strings, and data types; both operands must be the
same type. When acting on data types, a less-than-or-equal-to comparison is
true
if the left operand is the same as the right operand or is
a subset of it.
>= (greater than or equal to)
Resolves to true
if the left operand is larger than or equal to the
right operand. Accepts numbers, strings, and data types; both operands must be the
same type. When acting on data types, a greater-than-or-equal-to comparison is
true
if the left operand is the same as the right operand or is
a superset of it.
=~ (regex or data type match)
Resolves to true
if the left operand matches the right operand.
Matching means different things, depending on what the right operand is.
This operator is non-transitive with regard to data types. The right operand must be one of:
A regular expression (regex), such as
/^[<>=]{7}/
.A stringified regular expression — that is, a string that represents a regular expression, such as
"^[<>=]{7}"
.A data type, such as
Integer[1,10]
.
true
if the string matches the regular expression.
If the right operand is a data type, the left operand can be any value. The
expression resolves to true
if the left operand has the specified
data type. For example, 5 =~ Integer
and 5 =~
Integer[1,10]
are both true
.
!~ (regex or data type non-match)
Resolves to false
if the left operand matches the right operand. So,
$x !~ $y
is the same as !($x =~ $y)
. It has
the same behavior and restrictions, but opposite result, as regex match
=~
, above.
in
Resolves to true
if the right operand contains the left operand. The
exact definition of "contains" here depends on the data type of the right operand.
See table below.
This operator is non-transitive with regard to data types. It accepts:
A string, regular expression, or data type as the left operand.
A string, array, or hash as the right operand.
Expression | How in expression is evaluated |
---|---|
String in String | Tests whether the left operand is a substring of the right,
ignoring
case:'eat' in 'eaten' # resolves to true 'Eat' in 'eaten' # resolves to true |
String in Array | Tests whether one of the members of the array is identical to
the left operand, ignoring
case:'eat' in ['eat', 'ate', 'eating'] # resolves to true 'Eat' in ['eat', 'ate', 'eating'] # resolves to true |
String in Hash | Tests whether the hash has
a key identical to the left operand, ignoring
case:'eat' in { 'eat' => 'present tense', 'ate' => 'past tense'} # resolves to true 'eat' in { 'present' => 'eat', 'past' => 'ate' } # resolves to false |
Regex in String | Tests whether the right operand matches the regular
expression:# note the case-insensitive option ?i /(?i:EAT)/ in 'eatery' # resolves to true |
Regex in Array | Tests whether one of the members of the array matches the
regular
expression:/(?i:EAT)/ in ['eat', 'ate', 'eating'] # resolves to true |
Regex in Hash | Tests whether the hash has a key that
matches the regular
expression:/(?i:EAT)/ in { 'eat' => 'present tense', 'ate' => 'past tense'} # resolves to true /(?i:EAT)/ in { 'present' => 'eat', 'past' => 'ate' } # resolves to false |
Data type in Array | Tests whether one of the members of the array matches the
data
type:# looking for integers between 100 and 199 Integer[100, 199] in [1, 2, 125] # resolves to true Integer[100, 199] in [1, 2, 25] # resolves to false |
Data type in anything else | Always false . |
Related information