String Comparison Operators

Contents | Up | Prev | Next

# 7.2. String Comparison Operators

In order to compare for string equality, or if one string is alphabetically bigger than another, you can use the six string comparison operators. Here are the string operators together with the numerical operators they correspond too:

String Operator | Numerical Operator
`eq` | `==`
`ne` | `!=`
`gt` | `>`
`lt` | `<`
`ge` | `>=`
`le` | `<=`

Notice that the string operators are built from the initials of their abbreviated names. (E.g: eq = equal, gt = greater than). Perl's string comparison is case-sensitive. If you want a case insensitive string comparison, use the `lc` function to convert the strings to lowercase beforehand.

Example:

    print "Please enter your private name:\n";
    $name = <>;
    chomp($name);
    if (lc($name) eq "rachel")
    {
        print "Your name is Rachel!\n";
    }
    else
    {
        print "Your name is not Rachel!\n";
    }

Contents | Up | Prev | Next