1. Computing

Comparison Operators - How to compare values in Perl

Comparison Operators - Greater Than, Greater Than or Equal To

By

Next: Comparison Operators - 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 seen how to see if two values are equal to, or not equal to each other.

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. Remember 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"; } 
Remember that when we talk about string values being greater than 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.

Next: Comparison Operators - Less Than, Less Than or Equal To

  1. About.com
  2. Computing
  3. Perl
  4. Perl Tutorials
  5. Comparison Operators - How to compare values in Perl

©2014 About.com. All rights reserved.