# Relationship Operations | Perl

To compare scalar data or the values of scalar variables, the Perl language offers a set of binary operations that calculate equality ratios greater than, greater than or equals, and so on, between their operands, so this group of operations is also called **relation operations**. Perl uses different operations to compare numeric data and string data. All of them are presented in Table. 4.1.

**Table 4.1**. Relationship operations.

Operation | Numeric | String | Meaning |
---|---|---|---|

Equality | == | eq | Truth if operands are equal, otherwise lies |

Inequality | != | ne | Truth if operands are not equal, otherwise lies |

Less | < | lt | Truth, if the left operand is smaller than the right operand, otherwise lies |

More | > | gt | Truth, if the left operand is larger than the right operand, otherwise the lie |

Less than or equal to | <= | le | Truth, if the left operand is greater than or equal to the right operand, otherwise lies |

Greater than or equal to | >= | ge | True, if the right operand is greater than or equal to the left operand, otherwise false |

Comparison | < › | cmp | 0 if the operands are 1, if the left operand is greater than the right -1, if the right operand is larger than the left operand |

The result of relationship operations (except for the last comparison) is True, value 1, or False, an empty string " ".

**The**

value of truth in arithmetic operations is interpreted as the number 1, and in string operations as the string "1". The value of false in arithmetic operations is interpreted as the number 0, and in string operations as an empty string " ".

### Numeric Relationship Operations

Numeric relationship operations are applied to numeric data, and one or both operands can be specified by a string containing the correct decimal number. In the numeric operations of a relationship, if any of the operands are specified by a string whose contents do not represent the correct decimal number, its value is taken to be about and a warning is displayed about the incorrect use of the operand in the numeric operation of the relationship (if the warning mode of the Perl interpreter is enabled). The meaning of relation operations for numeric data corresponds to the usual mathematical operations of comparing numbers (example 4.7).

123 > 89; # Result: 1 (true)

123 < 89; # Result: "" (false)

123 <= 89; # Result: "" (false)

89 <= 89; # Result: 1 (true)

23 >= 89; # Result: "" (false)

23 < › 89; # Result: -1 (right operand greater than left)

89 < › 23; # Result: 1 (right operand greater than left)

The use of numerical comparison operations is not difficult, but when comparing for equality of decimal numbers with floating point, rounding effects may appear associated with a limited number of significant digits in the mantissa of the representation of real numbers in the computer and leading to "incorrect", from the user's point of view, the work of comparison operations. Example 4.8 illustrates this situation.

#! peri-w

$z = 0.7;

$zz = 10+0.7-10; # Variable $zz contains the number 0.7

# Print the string "z is equal to zz" if $z and $zz are equal

print "z equals zz\n" if ($z == $zz);

If we try to run Example 4.8, we are surprised to find that our program will not print anything. What's the big deal? The clue lies in the operator for calculating the value of a variable $zz. When performing arithmetic operations, rounding errors result in a value of 0.699999999999999999 (you can insert a print operator of a variable $zz and make sure of this), although close to 0.7, but not equal to it exactly. Consequently, the comparison operation worked correctly!

**Tip**

Do not use the comparison operation for the equality of real numbers, its result may not correspond to what is expected from the point of view of mathematics. If it is necessary to check the equality of two real numbers, it is better to compare the absolute value of their difference with some very small number (depending on the required accuracy):

abs($a-$b) <= 0.00000001; # Check for equality

### Relationship string operations

The comparison of string data is based on its ordering according to the ASCII code table, that is, a character with a smaller ASCII code precedes a character with a large code. Rows are compared character-by-character from left to right. This means that if the first characters of the strings are equal, then the second ones are compared and if they are equal, then the third ones are compared, etc. Moreover, if the strings are of different lengths, then the missing number of characters with the code o. It should be noted that unlike some other programming languages in Perl, line-closing space characters are significant when comparing strings. Example 4.9 shows string comparisons illustrating the rules stated.

"A" It "a"; | # Result: true (code "A" – \101, code "a" – \141) |

"a" It "aa"; | # Result: true (the # character is added to line "a" with the code \000, which is less than the \141 # code \141 # of the second character "a" of the right operand string) |

"a" It "a "; | # Result: true (the character # is added to line "a" with the code \000, which is less than the code \040 # the trailing space of the line of the right operand) |

"12" It "9"; | # Result: true (code "1" – \061, code "9" – \071) |

" 9" eq "09"; | # Result: false (code " " – \040, code "O" – \060) |

Let's look at the last two operations of comparing string literals. The contents of their operands can be converted to the correct numbers, and therefore similar numeric relation operations apply to them. However, their result will be significantly different from the result of performing string operations on a relationship. If you use **the <** operation in the penultimate expression, the result is False, and if you apply the **==** operation in the last expression, the result is True. This should always be kept in mind, as Perl automatically converts character data to numeric data where necessary.