Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Answer: How do I compare two strings?

by turnstep (Parson)
on Mar 29, 2000 at 21:44 UTC ( #6437=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I compare two strings? - Answer contributed by turnstep

if ($string1 eq $string2){ print"\$string1 is equal to \$string2\n"; } elsif ($string1 lt $string2){ print"\$string1 is less than \$string2\n"; } elsif ($string1 gt $string2){ print"\$string1 is greater than \$string2\n"; }
== tests equality for numbers.
eq does the same for strings.
lt is the string testing equivalent of the numerical <
gt is the string testing equivalent of the numerical >

You can also use the cmp operator, which is the non-numerical equivalent of the <=> operator:

$result = $string1 cmp $string2;

$result will be:

  • 0 if the strings are equal
  • 1 if string1 is greater than string2
  • -1 if string1 is less than string2

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2016-12-05 03:18 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (71 votes). Check out past polls.