Science, Tech, Math › Computer Science A Beginner Guide to Comparing Values in Perl How to Compare Perl Values Using Comparison Operators Share Flipboard Email Print ermingut / Getty Images Computer Science Perl PHP Programming Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Kirk Brown Kirk Brown Computer Science Expert B.A., Abilene Christian University Kirk Brown is a systems software engineer with expertise in Perl, PHP, Ruby, and Python. Learn about our Editorial Process Updated on January 10, 2019 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 American Standard Code for Information Interchange (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. 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. Cite this Article Format mla apa chicago Your Citation Brown, Kirk. "A Beginner Guide to Comparing Values in Perl." ThoughtCo, Aug. 28, 2020, thoughtco.com/comparison-operators-compare-values-in-perl-2641145. Brown, Kirk. (2020, August 28). A Beginner Guide to Comparing Values in Perl. Retrieved from https://www.thoughtco.com/comparison-operators-compare-values-in-perl-2641145 Brown, Kirk. "A Beginner Guide to Comparing Values in Perl." ThoughtCo. https://www.thoughtco.com/comparison-operators-compare-values-in-perl-2641145 (accessed May 31, 2023). copy citation