Next: Sorting
Up: Comparisons
Previous: Numerical comparison
Contents
Index
String comparison
For comparing strings, you can use
- eq: equality
- ne: non-equality
- cmp: returns -1, 0, or 1, depending on if the
left argument is string-wise less than, equal to, or greater
than the right argument.
Note that string comparisons respect the case - a string
$var1 = 'NO' is not equal to a variable
$var2 = 'no'. If you want to compare two
strings and not worry about the case, the operators
uc (for converting a string to all upper-case)
and lc (for converting a string to all lower-case)
may be useful:
my $answer = 'YES';
if (lc $answer eq 'yes') {
print "You answered in the affirmative";
}