|
|
|
Join the Discussion
|
C/C++ from Perl NATXIE
| |
|
|
|
To compare string values in Perl, you must use the string
comparison operators: "eq, ne, lt, gt, le, ge". That way, Perl
knows it's supposed to compare the strings, and will not
attempt to convert them into numbers first. By the way, Perl
uses ASCII or Unicode to compare string values.
Here is a table of Perl's string comparison operators with
some examples to show you how they're used. For all cases,
assume that $x = 'a'.
Comparison |
String Operators |
Example |
Result |
Equal to |
eq |
$x eq 'a' |
true |
Not equal to |
ne |
$x ne 'b' |
true |
Less than |
lt |
$x lt 'b' |
true |
Greater than |
gt |
$x gt 'b' |
false |
Less than or equal to |
le |
$x le 'b' |
true |
Greater than or equal to |
ge |
$x ge 'b' |
false |
Comparison and interpolation
By itself, comparison really isn't that exciting. Been
there, done that, big yawn. However, because Perl uses these
operators to decide how to interpret the value in a scalar
variable, you do need to know how that happens. Basically, if
you use the numeric operators to compare two scalars, then
both the values are treated as numbers and then compared. If
you use the string operators, then they're treated as strings
and compared. So you see, knowing how Perl interpolates values
is critical to understanding what will happen in conditional
tests.
Converting numbers to strings
This one's easy: just use quotes. Having said that, I now
need to clarify that once you do you can't always get the
correct numeric value back from the string.
Sometimes underscores are used to make numbers easier to
read (you can't use commas because they're reserved for
separating lists): $x = 1_000_000; However, when this is converted to a
string, the underscore characters do not get converted back
into numbers. In other words, if $x = 1_000_000;
$y = 1_000_000;
then if($x == $y) will return true.
However, if $x = 1_000_000;
$y = '1_000_000';
it will not. Perl thinks it is comparing 1_000_000 to 1.
Why? Because any non-numeric characters following a number
aren't converted. Nor does Perl convert binary, hexadecimal or
octal numbers into their decimal counterparts. We'll discuss
that in detail in the next section, but knowing this much will
help you understand that if you use quotes around numbers with
underscores (1_000), hexadecimal numbers (0xFF), octal numbers
(037), or exponential numbers (3.55E300), then they won't
behave as you expect if you use numeric comparison operators
to compare them.
Converting strings to numbers
When Perl tries to figure out the numeric value of a
string, things get a little more complicated. First, it only
tries to find a number at the beginning of a string. If it
doesn't find one, the whole string is considered to be zero.
Therefore, $x = "abc123"; will evaluate to zero in a numeric
comparison.
Also, Perl will only look at the first number in the
string, and stops after it finds it. That's why if $x = "1_000"; it will be converted to a value of 1.
Also, if $x = "10 20 30 40 50"; it will convert to the value
of 10.
The same principles apply if you use decimal points in the
string, such as "9.0". The value will returned as 9. Now, if
you're trying to compare the numeric values of 9 to 9.0, they
are going to pass as being equal. But since the string values
of "9" and "9.0" are not equal, I need to bring this to your
attention. It's important not to make assumptions when you're
comparing strings to strings and numbers to numbers. You have
to know the rules, and pay attention to them.
As mentioned previously, Perl will not convert binary,
hexadecimal, or octal numbers into decimals. If you need to
convert those numbers, use the hex() or oct() functions.
So how does all of this work?
Well, if you want to compare string values, use the string
operators. Use numeric operators if you want to compare the
numeric values.
Using the point made in the previous section, if $x = 9;
$y = 9.0;
then if($x == $y)
{
# Returns true, because it's a numeric comparison
# Numerically, 9 and 9.0 are equivalent
}
but if($x eq $y)
{
# Returns false because "9" is not the same string as "9.0"
# Remember that because you're using a string comparison
# operator, Perl is going to do the conversion for you.
}
and if($x = $y)
{
# Returns true because there's only one '=' sign
# This assigns the value of 9.0 to $x
# (Bang head on desk several times...)
}
Summary
So you see, the only complicated thing about all of this is
remembering to use the right set of operators to compare
values. Understanding the rules of interpolation is the real
key to knowing how strings and numbers get converted back and
forth. If you understand that, then the rest is easy.
Print
this article
Previous
Features
|