Open In App

Perl | Comparing Scalars

Last Updated : 06 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 are 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 are compared and show how it works for numeric and string scalars differently.
    Example 1: 

Perl




# Perl program to illustrate
# == operator
   
# taking two numeric scalars
$x = 5;
$y = 5;
   
# using "==" operator
if($x == $y)
{
    print "== works with numeric value!";
}


Output: 

== works with numeric value!

Example 2:

Perl




# Perl program to illustrate
# == and eq operator
   
# string scalar
$str = "geekforgeeks";
   
if($str == "GEEKSFORGEEKS")
{
    print "== doesn't work with string values!";
   
}
   
# comparing with capital string
if($str eq "GEEKSFORGEEKS")
{
    print "eq works with string values!";
}


Output: 

== doesn't work with string values!

Explanation: In the output of example 2, the 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 the 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




# Perl program to demonstrate the 
# != operator
   
# numeric scalars
$x = 5;
$y = 10;
   
# using != operator
if($x != $y)
{
    print "!= works with numeric value!";
}


Output: 

!= works with numeric value!

Example 2:

Perl




# Perl program to demonstrate the 
# != and ne operator
   
# string scalar
$str = "geekforgeeks";
   
# using != operator
if($str != "GEEKSFORGEEKS")
{
    print "\n!= doesn't work with string values!";
   
}
   
# comparing with capital string
if($str ne "GEEKSFORGEEKS")
{
    print"ne works with string values!";
}


Output: 

ne works with string values!

Explanation: In the second example, the first print statement will not be executed because != converts both strings to 0. Hence, != works fine for numeric values but fails in the 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




# 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 ($x lt $y) )
{
    print "< and lt works with numeric value!";
}
   
if(($y > $x) and ($y gt $x) )
{
    print "\n> and gt works with numeric value!";
}


Output: 

< and lt works with numeric value!
> and gt works with numeric value!

Example 2:

Perl




# 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 string
if($str lt "GEEKSFORGEEKSZZZ")
{
    print"lt works with string values!";
}
   
# comparing with capital string
if($str gt "GEEKSFORGEEKSZZZ")
{
    print"gt works with string values!";
}
   
# comparing with capital string
if($str lt "kEEKSFORGEEKS")
{
    print"\nlt works with string values!";
}


Output: 

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 the 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 the second example’s output 
Perl’s string operator only first check the first character of String and 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 are checked in the case of string operators. The value is checked in the case of numeric operators.
    Example:

Perl




# 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 (($str le "keeksforgeeks") and ($str ge "feeksforgeeks"))
{
    print "\nle and ge works!";
}


Output: 

<= and>= works
le and ge works!

Important Points to Remember:

  • The 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 string. And hence it will be true in case of ==, >= or <= as shown in the below example: 

Perl




# Perl program to illustrate 
# above point
   
# numeric scalars
$x = "BBB";
$y = "aaa";
   
if (($x == $y and ($x <= $y) and ($x >= $y)))
{
    print "True";
}


Output: 

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 the numeric comparison operator converted the string to 0. 

  • The string operator doesn’t compare numeric values, instead, it compares their 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




# Perl program to illustrate 
# above point
   
# numeric scalar
$x = 9;
$y = 17;
   
if ($x gt $y)
{
    print "True";
}


Output: 

True


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads