Perl 字符串等式运算符示例
以下是股权经营者名单。 假设变量 $a 持有 "abc" 并且变量 $b 持有 "xyz" 然后,让我们检查以下字符串相等运算符 −
| 序号 | 运算符 & 描述 |
|---|---|
| 1 | lt 如果左参数按字符串小于右参数,则返回 true。 示例 − ($a lt $b) 是 true。 |
| 2 | gt 如果左参数按字符串大于右参数,则返回 true。 示例 − ($a gt $b) 是 false。 |
| 3 | le 如果左参数按字符串小于或等于右参数,则返回 true。 示例 − ($a le $b) 是 true。 |
| 4 | ge 如果左参数按字符串大于或等于右参数,则返回 true。 示例 − ($a ge $b) 是 false。 |
| 5 | eq 如果左参数在字符串上等于右参数,则返回 true。 示例 − ($a eq $b) 是 false。 |
| 6 | ne 如果左参数按字符串方式不等于右参数,则返回 true。 示例 − ($a ne $b) 是 true。 |
| 7 | cmp 返回 -1、0 或 1,具体取决于左参数是按字符串方式小于、等于还是大于右参数。 示例 − ($a cmp $b) 是 -1。 |
示例
试试下面的例子来理解 Perl 中所有可用的字符串相等运算符。 将以下 Perl 程序复制并粘贴到 test.pl 文件中并执行该程序。
#!/usr/local/bin/perl
$a = "abc";
$b = "xyz";
print "Value of \$a = $a and value of \$b = $b\n";
if( $a lt $b ) {
print "$a lt \$b is true\n";
} else {
print "\$a lt \$b is not true\n";
}
if( $a gt $b ) {
print "\$a gt \$b is true\n";
} else {
print "\$a gt \$b is not true\n";
}
if( $a le $b ) {
print "\$a le \$b is true\n";
} else {
print "\$a le \$b is not true\n";
}
if( $a ge $b ) {
print "\$a ge \$b is true\n";
} else {
print "\$a ge \$b is not true\n";
}
if( $a ne $b ) {
print "\$a ne \$b is true\n";
} else {
print "\$a ne \$b is not true\n";
}
$c = $a cmp $b;
print "\$a cmp \$b returns $c\n";
当上面的代码被执行时,它会产生下面的结果 −
Value of $a = abc and value of $b = xyz abc lt $b is true $a gt $b is not true $a le $b is true $a ge $b is not true $a ne $b is true $a cmp $b returns -1

