Prerequisite: Scalars in Perl
Perl has two types of comparison operator sets. Just like other mathematical operators, instead of performing operations, these operators compare scalars. There are two types of sets of Perl comparison operators.
One is for numeric scalar values and one is for string scalar values. Both the types is illustrated in below table:
| Numeric | String | Description |
|---|---|---|
| == | eq | Equals to |
| != | ne | Not Equals to |
| < | lt | Is less than |
| > | gt | Is greater than |
| <= | le | Is less than or equal to |
| >= | ge | Is greater than or equal to |
Explanations for above Numeric and String Scalars Comparison Operators:
-
== and eq : This operator is used to check the equality. In the following code, outputs of codes after using == and eq is compared and showing how it work for numeric and string scalars differently.
Example 1:
# Perl program to illustrate# == operator# taking two numeric scalars$x= 5;$y= 5;# using "==" operatorif($x==$y){print"== works with numeric value!";}chevron_rightfilter_noneOutput:
== works with numeric value!
Example 2:
# Perl program to illustrate# == and eq operator# string scalar$str="geekforgeeks";if($str=="GEEKSFORGEEKS"){print"== doesn't work with string values!";}# comparing with capital stringif($streq"GEEKSFORGEEKS"){print"eq works with string values!";}chevron_rightfilter_noneOutput:
== doesn't work with string values!
Explanation : In the output of example 2, Last print statement will not be executed because $str and GEEKSFORGEEKS aren’t equal. Also, ASCII codes of ‘g’ and ‘G’ are different. Hence, == works fine for numeric values but fails in case of string values and eq works fine for String scalars only.
- != and ne
In the following code there is a comparison of outputs after using != and ne and showed which one works properly for string and which one works properly for numeric scalar values.
Example 1:
# Perl program to demonstrate the# != operator# numeric scalars$x= 5;$y= 10;# using != operatorif($x!=$y){print"!= works with numeric value!";}chevron_rightfilter_noneOutput:
!= works with numeric value!
Example 2:
# Perl program to demonstrate the# != and ne operator# string scalar$str="geekforgeeks";# using != operatorif($str!="GEEKSFORGEEKS"){print"\n!= doesn't work with string values!";}# comparing with capital stringif($strne"GEEKSFORGEEKS"){print"ne works with string values!";}chevron_rightfilter_noneOutput:
ne works with string values!
Explanation: In the second example, first print statement will not be executed because != converts both string to 0. Hence, != works fine for numeric values but fails in case of string values and ne works fine for string scalars.
- (> or gt) And (< or lt)
In the below codes, we will compare outputs after using (> or gt) and (< or lt) and will see which one works properly for string and which one works properly for numeric scalar values.
Example 1:
# Perl program to demonstrate the# (> or gt) And (< or lt)# operator# numeric scalars$x= 4;$y= 5;# using (> or gt) And (< or lt)if(($x<$y) and ($xlt$y) ){print"< and lt works with numeric value!";}if(($y>$x) and ($ygt$x) ){print"\n> and gt works with numeric value!";}chevron_rightfilter_noneOutput:
< and lt works with numeric value! > and gt works with numeric value!
Example 2:
# Perl program to demonstrate the# (> or gt) And (< or lt)# operator# string scalar$str="geekforgeeks";if($str<"GEEKSFORGEEKS"){print"< doesn't work with string values!";}# comparing with capital stringif($strlt"GEEKSFORGEEKSZZZ"){print"lt works with string values!";}# comparing with capital stringif($strgt"GEEKSFORGEEKSZZZ"){print"gt works with string values!";}# comparing with capital stringif($strlt"kEEKSFORGEEKS"){print"\nlt works with string values!";}chevron_rightfilter_noneOutput:
gt works with string values! lt works with string values!
Explanation: The above code tells us some interesting things about how Perl works with strings. The first example’s output is very obvious since both string and numeric operators treat Numeric scalars same way.
But in the second Output, the “lt” didn’t behave as we expected. Let’s say the Perl “lt” operator is not case sensitive, but we even put “ZZZ” after that and even in that case $str is not less than the string in quotes and the next output showed that it was greater. This point can be clear from the second line of second example’s output
Perl’s string operator only first check the first character of String and it compares ASCII codes. Since block letters come first in the ASCII table. The Perl compiler matched the first letter and then matched the rest. - (>= or ge) And (<= or le)
These operators also work on the ASCII values which checked in case of string operators. The value is checked in the case of numeric operators.
Example:
# Perl program to demonstrate the# (>= or ge) And (<= or le)# operator# numeric scalars$x= 5;$y= 10;if(($x<=$y) and ($y>=$x)){print"<= and>= works";}# string scalar$str="geeksforgeeks";if(($strle"keeksforgeeks") and ($strge"feeksforgeeks")){print"\nle and ge works!";}chevron_rightfilter_noneOutput:
<= and>= works le and ge works!
- Numeric operator will always convert String values to 0. When we compare two string scalars with Numeric operators like ==, >= or <= then it will always convert the scalars to 0 or 0.0. Since they are not a string. And hence it will be true in case of ==, >= or <= as shown in the below example:
# Perl program to illustrate# above point# numeric scalars$x="BBB";$y="aaa";if(($x==$yand ($x<=$y) and ($x>=$y))){print"True";}chevron_rightfilter_noneOutput:
True
Explanation: In the above code “aaa” is less than BBB in every aspect (Lower case and also ASCII of a is greater than B) but still both strings will be equal since numeric comparison operator converted the string to 0.
- String operator doesn’t compare numeric values, instead it compares there ASCII values. String operators compare ASCII values for numeric values. In the following example “9 gt 17” is true but “17 gt 9” will give the result as false.
# Perl program to illustrate# above point# numeric scalar$x= 9;$y= 17;if($xgt$y){print"True";}chevron_rightfilter_noneOutput:
True
- Perl | Scalars
- Comparing content of files using Perl
- Perl Tutorial - Learn Perl With Examples
- Perl | Basic Syntax of a Perl Program
- Perl vs C/C++
- Perl | next operator
- Perl | last in loop
- Perl | tr Operator
- Perl | y Operator
- Perl | tell() Function
- Perl | ne operator
- Perl | eq operator
- Perl | int() function
- Perl | gt operator
- Perl | exp Function
- Perl | cmp Operator
- Perl | hex Function
- Perl | each() Function
- Perl | uc() Function
- Perl | ge operator
Important Points to Remember:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh

