Perl Variables: Comparing Scalars
April 26, 1999
Size counts, often, when you must weigh one value against
another. Returning to our currency conversion example, you
may wish to determine whether the Canadian dollar figure is
a greater or lesser number than its American dollar equivalent.
When you compare two values in Perl, the result is either true
or false. Using comparison operators you specify the criterion
for the comparison.
For example, suppose that $USTotal contains the value 50 and
$CDNTotal possesses 67.5. Using the arithmetic greater-than
operator (>):
$USTotal > $CDNTotal
|
yields
|
false
|
$CDNTotal > $USTotal
|
yields
|
true
|
Typically, you would include one of these comparisons inside
a larger expression, which makes use of the result. A simple
example would be an assignment to a third variable:
$USgreater=($USTotal>$CDNTotal);
In a later line of code you might test whether $USgreater
contained true or false, as a result of the above comparison.
More commonly, though, these types of comparisons are used in
conditional statements to direct the flow of the Perl program.
We haven't yet looked at conditional statements, but they should
be quite familiar to readers with basic programming experience.
Non-programmers can simply read the example aloud to get
an idea of the logic behind this code:
if ($USTotal>$CDNTotal)
{ ...execute some piece of code... }
else
{ ...execute some other piece of code... }
Now suppose that we wanted to test whether $USTotal and
$CDNTotal were equal. Using the arithmetic equality operator,
a double equal sign (==):
$USTotal == $CDNTotal
|
yields
|
false
|
Of course, if both $USTotal and $CDNTotal contained the
same value, the result of this equality comparison would
have been true. Programmers note: the arithmetic
comparison operator is a double equal sign, not just one.
Several other programming languages use a single equal sign
to represent both the comparison operator and the assignment
operator, and forgetting to use the double equal sign is a
common cause of program errors in Perl scripts.
Recall our earlier discussion about data types, and how Perl
infers the data type based on the operator being used. In
the above example, we are comparing whether one value is
greater than another -- this only makes sense with numbers,
since we cannot say that "dog" is greater than
"cat" (well, some might say that, but they would
be asking for trouble). In fact, Perl can make this
comparison, but it does so using different logic and different
operators.
As far as Perl is concerned, "dog" is greater than
"cat" because it comes after cat alphabetically.
Conversely, we can say that "cat" is less than
"dog". The string greater-than operator in Perl
is "gt", and the string equality operator is
"eq", thus if $petA contained "cat"
and $petB contained "dog" we could compare:
$petA gt $petB
|
yields
|
false
|
$petA eq "cat"
|
yields
|
true
|
There are many possible reasons to compare variables --
this tutorial is not in-depth enough to explore these in
detail. The table below summarizes the common comparison
operators, which you can use when and if you encounter a
situation where such a comparison would be useful.
Table 1. Basic Perl Operators
The Perl You Need to Know
Table 2. Perl Comparison Operators
|