Related Articles

# Perl | Comparing Scalars

• Last Updated : 25 Nov, 2019

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

NumericStringDescription
==eqEquals to
!=neNot Equals to
<ltIs less than
>gtIs greater than
<=leIs less than or equal to
>=geIs 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 "==" operator``if``(``\$x` `== ``\$y``)``{``    ``print` `"== works with numeric value!"``;``}`

Output:

```== 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 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, 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 != operator``if``(``\$x` `!= ``\$y``)``{``    ``print` `"!= works with numeric value!"``;``}`

Output:

```!= works with numeric value!
```

Example 2:

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

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:
 `# 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 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` `(``\$x` `gt ``\$y``)``{``    ``print` `"True"``;``}`

Output:

```True
```

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.75435597,"math_prob":0.95559627,"size":6576,"snap":"2021-31-2021-39","text_gpt3_token_len":1775,"char_repetition_ratio":0.18715765,"word_repetition_ratio":0.3043478,"special_character_ratio":0.29121047,"punctuation_ratio":0.12154233,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9974781,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-22T16:02:03Z\",\"WARC-Record-ID\":\"<urn:uuid:e7a40d72-0825-43ee-a691-79be5661a65e>\",\"Content-Length\":\"142136\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:63c0e6d1-ce91-4574-854d-6130d4c4d70e>\",\"WARC-Concurrent-To\":\"<urn:uuid:ffa38a83-d2b2-409b-8230-a1c5b9a8c6b4>\",\"WARC-IP-Address\":\"184.51.150.50\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/perl-comparing-scalars/\",\"WARC-Payload-Digest\":\"sha1:YERPSEU72RMLNPRPZGAABUZ6SKHLFR53\",\"WARC-Block-Digest\":\"sha1:MGRLL6K4PYEJAWTCDLKO6XRV2CTGV7ME\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057366.40_warc_CC-MAIN-20210922132653-20210922162653-00422.warc.gz\"}"}