Pathologically Eclectic Rubbish Lister PerlMonks

### How do I compare two strings?

by vroom (Pope)
 on Feb 01, 2000 at 07:39 UTC 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"; } [download]``` == 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; [download]``` \$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"; } [download]``` 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 } [download]``` 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.

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 Discipulus stopped with turkisk spices anyway.. but better hashes than arrays! [oiskuu]: Discipulus, from the edge or from the top? [Discipulus]: oiskuu from the top aka 0-0 coords [LanX]: edge or node? [oiskuu]: from each height difference, there's a fixed number of "moves right" - "moves left". So it's a number of combinations. [Discipulus]: I already know how much distinct paths exists but to demonstrate the property i want to have them all [oiskuu]: ie C(n, k); where n is height difference, k is position in the line [Discipulus]: infact there are 70 distinct path to reach the tale wich holds 70

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2018-03-19 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (239 votes). Check out past polls.

Notices?