How to Compare Values in Perl Using Comparison Operators

How to Compare Perl Values Using Comparison Operators

Programming language
Getty Images/ermingut

Perl comparison operators can sometimes be confusing to new Perl programmers. The confusion stems from the fact that Perl actually has two sets of comparison operators - one for comparing numeric values and one for comparing string (ASCII) values. 

Since comparison operators are typically used to control logical program flow and make important decisions, using the wrong operator for the value you are testing can lead to bizarre errors and hours of debugging, if you're not careful.

Note: Don't forget to catch what's written at the very bottom of this page for some last-minute things to remember.

Equal, Not Equal

The simplest and probably most used comparison operators test to see if one value is equal to another value. If the values are equal, the test returns true, and if the values are not equal, the test returns false.

For testing the equality of two numeric values, we use the comparison operator ==. For testing the equality of two string values, we use the comparison operator eq (EQual).

Here's an example of both:

 if (5 == 5) { print "== for numeric values\n"; }
 if ('moe' eq 'moe') { print "eq (EQual) for string values\n"; } 

Testing for the opposite, not equal, is very similar. Remember that this test will return true if the values tested are not equal to each other. To see if two numeric values are not equal to each other, we use the comparison operator !=. To see if two string values are not equal to each other, we use the comparison operator ne (Not Equal).

 if (5 != 6) { print "!= for numeric values\n"; }
 if ('moe' ne 'curly') { print "ne (Not Equal) for string values\n"; } 

Greater Than, Greater Than or Equal To

Now let's look at the greater than comparison operators. Using this first operator, you can test to see if one value is greater than another value.

To see if two numeric values are greater than each other, we use the comparison operator >. To see if two string values are greater than each other, we use the comparison operator gt (Greater Than).

 if (5 > 4) { print "> for numeric values\n"; }
 if ('B' gt 'A') { print "gt (Greater Than) for string values\n"; } 

You can also test for greater than or equal to, which looks very similar. Keep in mind that this test will return true if the values tested are equal to each other, or if the value on the left is greater than the value on the right.

To see if two numeric values are greater than or equal to each other, we use the comparison operator >=. To see if two string values are greater than or equal to each other, we use the comparison operator ge (Greater-than Equal-to).

 if (5 >= 5) { print ">= for numeric values\n"; }
 if ('B' ge 'A') { print "ge (Greater-than Equal-to) for string values\n"; } 

Less Than, Less Than or Equal To

There are a variety of comparison operators you can use to determine the logical flow of your Perl programs. We've already discussed the difference between the Perl numeric comparison operators and the Perl string comparison operators, which can cause some confusion to new Perl programmers.

 We've also learned how to tell if two values are equal to, or not equal to each other, and we've learned how to tell if two values are greater than or equal to each other.

Let's look at the less than comparison operators. Using this first operator, you can test to see if one value is less than another value. To see if two numeric values are less than each other, we use the comparison operator <. To see if two string values are less than each other, we use the comparison operator lt (Less Than).

 if (4 < 5) { print "< for numeric values\n"; }
 if ('A' lt 'B') { print "lt (Less Than) for string values\n"; } 

You can also test for, less than or equal to, which looks very similar. Remember that this test will return true if the values tested are equal to each other, or if the value on the left is less than the value on the right.

To see if two numeric values are less than or equal to each other, we use the comparison operator <=. To see if two string values are less than or equal to each other, we use the comparison operator le (Less-than Equal-to).

 if (5 <= 5) { print "<= for numeric values\n"; }
 if ('A' le 'B') { print "le (Less-than Equal-to) for string values\n"; } 

More Information on Comparison Operators

When we talk about string values being equal to each other, we're referring to their ASCII values. So, the capital letters are technically less than the lowercase letters, and the higher the letter is in the alphabet, the higher the ASCII value.

Make sure you check your ASCII values if you're trying to make logical decisions based on strings.