Perl cmp 操作符
Perl中的cmp运算符是一个字符串等价比较运算符,用于比较在该运算符左右两边的两个字符串是否相等或小于另一个。
语法: string1 cmp string2
返回: 如果string1小于,则 返回 -1;如果等于,则 返回 0;如果大于string2,则 返回 1。
例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 returnsc");
输出。
Comparison of a andb 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 returnsc");
输出。
Comparison of a andb 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 returnsc");
输出。
Comparison of a andb returns 1