In the past article, we have talked about the Arithmetic, Assignment, Increment/Decrement, and Range operators. In this article, we are going to complete our talk about operators.
Now, a very important type of operators: The Comparison operators. Comparison operators are essential for decision making. They compare numeric values, and return true if the condition is met. The following table lists the supported comparison operators in Perl:
|==||Equality operators. It checks whether the left side operand is equal to the right side one.|
|<||Less than. It checks whether the left side operand is less than the right side one.|
|<=||Less than or Equal. It checks whether the left side operand is less than or equal to the right side operand.|
|>||Greater than. It checks whether the left side operand is greater than the right side one.|
|>=||Greater than or equal. It returns true if the left side operand is greater than or equal to the right side one.|
|!=||Inequality operator. It returns true if the two operands are not equal.|
|<=>||The complex comparison operator. It checks its two operands and returns:
If you have studied Logic, you may have some knowledge of the Logic gates. There are three basic logic gates: AND, OR, and NOT. Consider two electrical switches connected together in series. For the current to flow from point to another across the two switches, both switches need to be ON. If one of them is OFF, the current won’t flow, and the result will be OFF. On the other hand, if the switches were in parallel, for the current to flow, only one switch needs to be ON. The third gate: the NOT gate inverts its input.
This is the concept of the logic gates in brief. The Logical operators do this job in software programs. The following table lists the logical operators supported in Perl:
|The Logical AND operator.
It returns true if (and only if) both operands are true.
|The Logical OR operator.
This operator returns true if either if its operands are ture.
|The Logical NOT operator.
It inverses its input.
- checks the entered number (which is stored in the variable $a) if it is greater than 0
- checks if the entered number is less than or equal to 10
- performs a logical AND between the results of the two above comparisons.
- The true value in Perl can be represented by 1 (or non-zero value), while the empty string “” represents the false value.
For strings concatenation and repetition, there are two operators:
- The Concatenation operator “.”
This operator concatenates (connects) two strings together to form a larger string.
- The String Repetition Operator “x”
This operator takes two operands: the first is the string to repeat, and the other is the number of times to repeat the provided string. The result of the operation is a string that consists of repeating the first operand (string) the specified number of times.
In this example, we are going to write a script that accepts the first name and the last name (as two separate inputs) from the user, and prints the full name.
Line 8: uses the concatenation operator “.” to concatenate the string variable $firstname, a white space, and $lastname. The result of the concatenation is then assigned to the variable $fullname.
The following statement will create a string of “=” (could be used in formatting a table-like output)
$str_line = "=" x 30;
When printed using the print function, we get the following output:
String Comparison Operators
As with numbers, Strings also have their comparison operators. I will mentioned here the two most important operators of this type: Equality and Inequality operators.
String Equality Operator eq
The string equality operator returns true if both operands (strings) are exact match.
($str1 eq #str2)
String Inequality Operator ne
This is the opposite of the String Equality operator “eq”. It returns true if the left side operand is NOT equal to the right side one.
($str1 ne #str2)
$country = "India";
The following comparison will return false.
($country eq "Brazil")
On the other hand, this expression will return true:
($country ne "india")
Notice that “India” doesn’t match “India”. That is why the inequality operator returns true when comparing them. The reason is clear, Perl is case sensitive.
* * * * * *
- In this article, we have talked about the Comparison, Logical, and String operators.
- Comparison operators are used to compare numeric values. They return true if the specified condition is met. They are essential for decision making.
- Logical operators do the job of logic gates in computer programs.
- String operators are useful when we need to concatenate two strings, and when a string is to be repeated for a specific number of times.
- String comparison operators compare two strings, and return true if the condition is met.
In the next article, we will build upon what we have learned in this article. We will talk about Decision Making. An important topic that you should not miss. So, stay here, and don’t go anywhere!!