← Back to Perl Course | Chapter 3: Strings | Lesson 3 of 7

Strings की तुलना

Strings math symbols की बजाय eq और lt जैसे word-like operators इस्तेमाल करती हैं।

Strings Compare करना

String comparison operators हैं eq, ne, lt, gt, le, और ge, और cmp -1, 0, या 1 return करता है। ये character codes इस्तेमाल करके character by character compare करते हैं, इसलिए uppercase letters lowercase वालों से पहले sort होते हैं। non-numeric strings पर == जैसे numeric operators इस्तेमाल करना warnings produce करता है और इनकी numeric values compare करता है।

Note: दोनों sides को lc से lowercase करके case-insensitively compare करें।

उदाहरण: Comparing strings

perl
use strict;
use warnings;

print "eq: ", ("perl" eq "perl" ? "same" : "different"), "\n";
print "ne: ", ("perl" ne "Perl" ? "different" : "same"), "\n";
print "lt: ", ("apple" lt "banana" ? "yes" : "no"), "\n";
print "Zebra lt apple: ", ("Zebra" lt "apple" ? "yes" : "no"), "\n";
print "cmp: ", join(",", "a" cmp "b", "b" cmp "b", "c" cmp "b"), "\n";
print "case-insensitive: ", (lc("PERL") eq lc("perl") ? "equal" : "not equal"), "\n";
print "sorted: ", join(" ", sort qw(banana Apple cherry apple)), "\n";
print "sorted ignoring case: ", join(" ", sort { lc($a) cmp lc($b) or $a cmp $b } qw(banana Apple cherry apple)), "\n";

# Output:
# eq: same
# ne: different
# lt: yes
# Zebra lt apple: yes
# cmp: -1,0,1
# case-insensitive: equal
# sorted: Apple apple banana cherry
# sorted ignoring case: Apple apple banana cherry
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. strings compare करने के लिए == इस्तेमाल करना
  2. यह भूल जाना कि "Zebra" "apple" से पहले sort होता है
  3. lt की बजाय < से text compare करना
चैप्टर सारांश
  • eq और ne strings की equality test करते हैं
  • lt gt le ge strings order करते हैं
  • cmp -1, 0, या 1 return करता है
  • Uppercase lowercase से पहले sort होता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.