!=操作员不工作

时间:2014-11-14 22:22:17

标签: perl

我有以下if语句,'!='不起作用。这行代码中的第二个if语句不起作用,有人可以告诉我原因吗?

    if ($line =~ $search) {
        print "$line <br> <br>";     
    }
}
if ($line != $search) { #This if statement will not work
    print "word is not in file";
}

2 个答案:

答案 0 :(得分:3)

==和!=运算符将操作数比较为数字。字符串比较的运算符为eqne

参考:http://perldoc.perl.org/perlop.html#Equality-Operators

答案 1 :(得分:2)

实际上,如果你正在寻找=〜,

的反面

你不需要ne或eq,但是!〜就像在

中一样
if ( $line !~ $search )

另外,请注意$ search中的特殊字符(for regex)。

perl regex doc

相关问题