next up previous contents
Next: The Range Operator (..) Up: The Bitwise Operators Previous: The Bitwise Operators

Comparison operators for numbers and strings

Perl has different operators (relational and equality operators)for comparing numbers and strings. They are defined as follows:

Equality                 Numeric String
Equal == eq
Not Equal != ne
Comparison <=> cmp
Relational                 Numeric String
Less than < lt
Greater than > gt
Less than or equal <= le
Greater than or equal >= ge

In controlling the logic of a conditional expression logical operators are frequently required.

In Perl, The logical AND operator is && and logical OR is ||.

For example to check if a valid number exist in a variable $var you could do:

if ( ($var ne "0") && ($var == 0))
  { # $var is a number
  }

Since Perl evaluates any string to 0 if is not a number.

The numeric relational operators, listed above are used to test the relationship between two operands. You can see if one operand is equal to another, if one operand is greater than another, or if one operator is less than another.

Note It is important to realize that the equality operator is a pair of equal signs and not just one. Quite a few bugs are introduced into programs because people forget this rule and use a single equal sign when testing conditions.

Example: Using the <=> Operator

The number comparison operator is used to quickly tell the relationship between one operand and another. It is frequently used during sorting activities.

Witth the form op1 <=> op2 this operator returns 1 if op1 is greater than op2, 0 if op1 equals op2, and -1 if op1 is less than op2.

You may sometimes see the <=> operator called the spaceship operator because of the way that it looks.

In the following example op2.pl we: Set up three variables and rint the relationship of each variable to the variable

$lowVar =  8;

$midVar = 10;

$hiVar  = 12;


print($lowVar <=> $midVar, "\n");

print($midVar <=> $midVar, "\n");

print($hiVar  <=> $midVar, "\n");

The program produces the following output:

-1
0
1

The -1 indicates that $lowVar (8) is less than $midVar (10). The 0 indicates that $midVar is equal to itself. And the 1 indicates that $hiVar (12) is greater than $midVar (10).

The string relational operators are used to test the relationship between two operands. You can see if one operand is equal to another, if one operand is greater than another, or if one operator is less than another.

String values are compared using the ASCII values of each character in the strings. You will see examples of these operators when you read about control program flow in Chapter 7 "Control Statements." So, we'll only show an example of the cmp comparison operator here.

Example: Using the cmp Operator

The string comparison cmp operator acts exactly like the < => operator except that it is designed to work with string operands. The following example, cmp.pl, will compare the values of three different strings:

Set up three variables and Prints the relationship of each variable to the variable, much like the numeric example

$lowVar = "AAA";

$midVar = "BBB";

$hiVar  = "ccC";



print($lowVar cmp $midVar, "\n");

print($midVar cmp $midVar, "\n");

print($hiVar  cmp $midVar, "\n");

The program produces the following output:

-1
0
1

Notice that even though strings are being compared, a numeric value is returned. You may be wondering what happens if the strings have spaces in them. Let's explore that for a moment. cmp2.pl:

$firstVar = "AA";

$secondVar = " A";

print($firstVar cmp $secondVar, "\n");

The program produces the following output:

1

which means that "AA" is greater than " A" according to the criteria used by the cmp operator.


next up previous contents
Next: The Range Operator (..) Up: The Bitwise Operators Previous: The Bitwise Operators
dave@cs.cf.ac.uk