珀尔 | cmp 运算符
Perl 中的 cmp运算符是一个字符串相等运算符,用于比较放置在该运算符左右两侧的两个字符串是否相等或小于另一个。
Syntax: string1 cmp string2
Returns: -1 if string1 is less, 0 if equal and 1 if greater than string2.
示例 1:当 String1 小于 String2
#!/usr/local/bin/perl
# Initializing strings
$a = "Geeks";
$b = "Welcome";
# Comparing strings
$c = $a cmp $b;
# Printing the comparison result
print("Comparison of \$a and \$b returns $c");
输出:
Comparison of $a and $b returns -1
示例 2:当 String1 等于 String2
#!/usr/local/bin/perl
# Initializing strings
$a = "Welcome";
$b = "Welcome";
# Comparing strings
$c = $a cmp $b;
# Printing the comparison result
print("Comparison of \$a and \$b returns $c");
输出:
Comparison of $a and $b returns 0
示例 3:当 String1 大于 String2
#!/usr/local/bin/perl
# Initializing strings
$a = "Welcome";
$b = "Geeks";
# Comparing strings
$c = $a cmp $b;
# Printing the comparison result
print("Comparison of \$a and \$b returns $c");
输出:
Comparison of $a and $b returns 1