Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How do I compare two strings?

( #2637=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Feb 01, 2000 at 07:39 UTC
Q&A  > strings


Answer: How do I compare two strings?
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
Answer: How do I compare two strings?
contributed by PipTigger

As an addendum to turnstep's fine post: Another useful test is: ne ... as in:

if ($string1 ne $string2) { print "\$string1 is not equal to \$string2\n"; }

Just as: == tests equality between two numbers,
!= tests inequality for numbers and
eq tests strings for equality as
ne tests strings for inequality.
I often find it useful to test
if ($string0 ne "") { #use $string0 }
to verify that a string has a value even if it's == 0.
Answer: How do I compare two strings?
contributed by gridlock

Depends on what kind of comparision: Strings can be tested for the following
Equality - the eq operator
Inequality - the ne operator
Greater Than - the gt operator
Greater or Equal - the ge operator
Less than - the lt operator
Less of Equal - the le operator


Syntax:
$stringtest = ($string1 eq $string2);
Try replacing eq with the other operators.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2015-07-04 01:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (57 votes), past polls