📜  珀尔 | cmp 运算符

📅  最后修改于: 2022-05-13 01:55:25.040000             🧑  作者: Mango

珀尔 | cmp 运算符

Perl 中的 cmp运算符是一个字符串相等运算符,用于比较放置在该运算符左右两侧的两个字符串是否相等或小于另一个。

示例 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