简体   繁体   English

Perl,NE运算符不起作用

[英]Perl, ne operator not working

Can someone please tell me why my ne operator isn't working in the below if statement? 有人可以告诉我为什么我的ne运算符在以下if语句中不起作用吗? It was working but after saving my program, it has stopped working. 它正在工作,但是保存我的程序后,它已停止工作。 Any help would be greatly appreciated, cheers. 任何帮助将不胜感激,欢呼。

$inFile = "animals.txt";
open (IN, $inFile) or
die "Can't find file: $inFile";

@animallist = (<IN>);

foreach $line (@animallist)  
{
  if ($line =~ $search)
  {
    print "$line <br> <br>";     
  }
}
if ($line ne $search)
{
  print "$search isn't in the animal list";
}

print end_html;

You seem confused about what your program does, so I thought I'd just tell you. 您似乎对程序的功能感到困惑,所以我想告诉您。

$inFile = "animals.txt";
open (IN, $inFile) or die "Can't find file: $inFile";
@animallist = (<IN>);

# here you define a file name, open a file, and read all of the lines
# in the file into the array @animallist

foreach $line (@animallist) {
# here you iterate over all the lines, putting each line into $line
  if ($line =~ $search) {
    print "$line <br> <br>";     
  }
# here you perform the regex match: $line =~ /$search/ and if it 
# succeeds, print $line
}
# here you end the loop

if ($line ne $search) {
  print "$search isn't in the animal list";
}
# here you take the now uninitialized variable $line and try to match 
# it against the as of yet undefined variable $search
# If both of these variables are undefined, and you are not using warnings
# it will simply return false (because "" ne "" is false)
# without warning about undefined variables in ne

You should be aware that even if your entire line was, for example, cat , you still could not compare it using ne to the string cat , because when read from a file, it has a trailing newline, so it is really cat\\n . 您应该注意,即使您的整个行例如是cat ,您仍然无法使用ne与字符串cat进行比较,因为从文件中读取时,它具有尾随换行符,因此它实际上是cat\\n Unless you chomp it. 除非你chomp它。

It seems redundant to tell you, but of course you cannot check if the file does not contain $search after you finished reading the file. 告诉您似乎很多余 ,但您当然无法在完成读取文件后检查文件是否不包含$search You have to do that while reading the file. 您必须在读取文件时执行此操作。

Try using indentation so you can see when your blocks end at inappropriate places. 尝试使用缩进,以便您可以看到块何时在不适当的地方结束。

if ($line ne $search) isn't even within the foreach loop where you are populating and handling $line from the file. if($ line ne $ search)甚至不在foreach循环中,在该循环中您正在文件中填充和处理$ line。 I suggest putting it within the block to at least get the functionality I assume you are looking for. 我建议将它放在块中,至少可以得到我认为您正在寻找的功能。

It's hard to know what you expect to happen as we don't know what $search contains. 很难知道您期望发生什么,因为我们不知道$search包含什么。 Let's assume it's the name of an animal that is in your file. 假设它是文件中动物的名称。

When you execute your code, $search contains, say, "frog" and $line contains undef (as $line only contains data within your foreach loop). 执行代码时, $search包含“ frog”, $line包含undef (因为$line仅包含foreach循环内的数据)。 Those values aren't the same so the ne returns true and the message is printed. 这些值不相同,因此ne返回true并显示消息。

If you were to move that if statement into the foreach block then $line would have a value. 如果要将if语句移到foreach块中,则$line将具有一个值。 But it still wouldn't match as lines are read from a file with the newline character still attached. 但是它仍然不匹配,因为从文件中读取的行仍附加了换行符。 And "Frog" is not the same as "Frog\\n". 而且“青蛙”与“青蛙\\ n”不同。 You need to use chomp() to remove the newline from $line . 您需要使用chomp()$line删除换行符。

This is very similar to another recent question . 这与最近的另一个问题非常相似。 Are you working on the same piece of homework? 您在做同一份作业吗?

I think there are several points here: 我认为这里有几点:

  • For the if ($line ne $search), the $line is out of the scope of the foreach where it was declared and since you are not using strict, you won't get error and this condition should always be true. 对于if($ line ne $ search),$ line不在声明它的foreach范围内,并且由于您未使用strict,因此不会出错,并且此条件应始终为true。

  • I assume that $line has a newline so when you match this $line with $search, the condition can be true although the strings are not equal. 我假设$ line有换行符,所以当您将此$ line与$ search匹配时,条件可能为true,尽管字符串不相等。

Assuming that $line = "lion\\n" and $search = "lion" 假设$ line =“ lion \\ n”和$ search =“ lion”

then if you do if ($line =~ $search), the condition will be be true, because "lion" is part of the "lion\\n" string. 那么如果执行if($ line =〜$ search),则条件为true,因为“ lion”是“ lion \\ n”字符串的一部分。 This means that: if ($line eq $search) is false and if ($line ne $search) is true which I assume is your case. 这意味着:如果($ line eq $ search)为false,并且($ line ne $ search)为true,则我认为这是您的情况。

you can use the chomp($line) function to remove newlines from the end of the string. 您可以使用chomp($ line)函数从字符串末尾删除换行符。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM