Perl comparison operator
The comparison operator is an operator that compares two numbers. Perl has a numeric comparison operator which compares numbers and a string comparison operator which compares strings.
Numeric comparison operator
Numeric comparison operators are operators to compare values as numbers.
Operator | Meaning |
---|---|
A == B | A is equal to B |
A != B | A is not equal to B |
A > B | A is greater than B |
A >= B | A is greater than or equal to B |
A < B | A is less than B |
A <= B | A is less than or equal to B |
The comparison operator returns a true value if the condition is met, and a false value if the condition is not met.
See the following article for Perl boolean.
Example using if statement
Example using numeric comparison operators and if statement:
my $num1 = 4; my $num2 = 3; if ($num1 > $num2) { print "Left is big"; }
In this program, 4 is assigned to $num1 and 3 is assinged to $num2. Because $num1 is greater than $num2, the condition is true and the inside of the block is executed.
Example using ternary operator
Let's use the comparison operator with ternary operator.
my $num1 = 4; my $num2 = 3; my $message = $num1 > $num2 ? "OK": "Not OK";
Because $num1 is greater than $num2, true is returned and "OK" is assigned to $message.
String comparison operator
The string comparison operator is an operator used to compare strings in the dictionary order.
In dictionary order comparison, the index of the character code is compared one by one from the beginning of the string. "A" is less than "b". "1" is greater than "02".
operator | meaning |
---|---|
A eq B | A is equal to B |
A ne B | A is not equal to B |
A gt B | A is greater than B |
A ge B | A is greater than or equal to B |
A lt B | A is less than B |
A le B | A is less than or equal to B |
Example using if statement
This is an example that uses the string comparison operators with if statement.
my $str1 = "1"; my $str2 = "02"; if ($num1 gt $num2) { print "Left is big"; }
Join multiple conditions with logical operator
Logical operators allow you to join multiple comparison operators.
# $num1 is greater than 1 and less than 5 if ($num1 > 1 && $num1 < 5) { ... } # $num1 is less than 1 or greater than 5 if ($num1 < 1 || $num1 > 5) { ... }
Logical operators are explained here.
How to check the containing of the partial string
Perl's string comparison operator "eq" is used when the whole charaters of the strings match, but there are times when you want to know a partial match of a string. In that case, use a regular expression.
# If $str1 contains "Cat" if ($str1 =~ /Cat/) { ... }
Regular expressions are explained below.
Example program
This is an example of numerical comparison and string comparison.
use strict; use warnings; # Numeric comparison and string comparison print "1: Compare numerical comparison and string comparison\n"; my $num1 = "1"; my $num2 = "02"; if ($num1 < $num2) { print "Compare in the numeric order, $num1 is less than $num2\n"; } if ($num1 gt $num2) { print "Compare in the dictionary order, $num1 is greater than $num2\n"; }
Output
1: Compare numerical comparison and string comparison Compare in the numeric order, 1 is less than 02 Compare in the dictionary order, 1 is greater than 02