Perl | Comparing Scalars

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    == works with numeric value!
    

    Example 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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!";
    }

    chevron_right

    
    

    Output:

    == 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    != works with numeric value!
    

    Example 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program to demonstrte 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!";
    }

    chevron_right

    
    

    Output:

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program to demonstrte 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!";
    }

    chevron_right

    
    

    Output:

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

    Example 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program to demonstrte 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!";
    }

    chevron_right

    
    

    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 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl program to demonstrte 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!";
    }
         

    chevron_right

    
    

    Output:

    <= and>= works
    le and ge works!
    
    • Important Points to Remember:

      • 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:
        filter_none

        edit
        close

        play_arrow

        link
        brightness_4
        code

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

        chevron_right

        
        

        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 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.
        filter_none

        edit
        close

        play_arrow

        link
        brightness_4
        code

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

        chevron_right

        
        

        Output:

        True
        


      My Personal Notes arrow_drop_up

      Check out this Author's contributed articles.

      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.




      Article Tags :
      Practice Tags :


      Be the First to upvote.


      Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.