简体   繁体   English

在此Perl脚本中使用chomp

[英]Use of chomp in this Perl script

I am trying to learn Perl here and the tutorial suggests the following code snippet after reading some file: 我试图在这里学习Perl 并且阅读一些文件后,该教程建议以下代码段:

my $team_number = 42;
my $filename = 'input.txt';

open(my $fh, '<', $filename) or die "cannot open '$filename' $!";

my $found;
while(<$fh>) {
  chomp;
  last if($_ eq "Team $team_number");
}
die "cannot find 'Team $team_number'" if(eof $fh);

I don't quite understand why we need chomp, though. 我不太明白为什么我们需要排骨。 chomp removes the new line. chomp删除新行。 So in each loop we remove the new line at the end of the line, but why? 因此,在每个循环中,我们在行尾都删除了新行,但是为什么呢? The script does throw an error if you remove the chomp, but I don't understand why. 如果删除该脚本,脚本确实会引发错误,但是我不明白为什么。

Please re-consider down-voting : I understand what chomping does, but I didn't get why that was necessary to match since we're already looping through all the lines - why would you then need to remove the new line? 请重新考虑不赞成投票的方式 :我了解排字法的作用,但由于我们已经遍历所有行,我不明白为什么要进行排字法匹配-那么您为什么还要删除新行? It was only after reading Kallol's post that I realised that eq would fail if not chomped, because the actual string that is looped, then, is Team 42\\n. 只是在阅读了Kallol的文章之后,我才意识到,如果不加限制,eq将会失败,因为循环的实际字符串就是Team 42 \\ n。 So that's why my question is different from other chomp questions: I didn't know why my code wasn't working withouit chomp and someone pointed me in the right direction. 所以这就是为什么我的问题与其他chomp问题不同的原因:我不知道为什么我的代码无法与chomp一起使用,并且有人向我指出了正确的方向。

It is reasonable that you don't want the new line when you load text and are working with it. 加载文本并使用它时,不希望使用新行是合理的。 The new lines organize text into a file, in memory you have arrays or other data structures. 新行将文本组织到一个文件中,在内存中您具有数组或其他数据结构。 You don't have to chomp it, you generally want to. 不必 chomp它,你通常希望。

Why the code doesn't do what you intended without the chomp has been explained by Kallol and Sobrique : a new-line remains in $_ which thus doesn't match (the string without a new line) so the file is read to the end and eof returns true. 为什么代码没有你打算什么没有chomp已经被解释KallolSobrique :新行保持在$_其中因此不匹配(而不换行字符串),因此该文件被读取到end和eof返回true。 See chomp (or perldoc -f chomp ). 请参见chomp (或perldoc -f chomp )。 I don't see that the script "throws an error" though, as you say. 正如您所说,我看不到脚本“引发错误”。 It doesn't for me, with or without `chomp'; 不管有没有“ chomp”,这都不适合我; it just never matches. 它永远不会匹配。

And there is another error even with chomp , as noted by 123 : if the matching string is the last line in the file, by the time the match is found the file has been read so eof returns true and you get a statement that the string was not found. 而且即使使用chomp也存在另一个错误,如123所示 :如果匹配的字符串是文件中的最后一行,则在找到匹配项时已读取文件,因此eof返回true,并且您得到一条声明,表明该字符串没有找到。 That you could fix by using a flag, for example (which you have right there), in which case you wouldn't be deciding via eof any more. 例如,您可以通过使用一个标志(在那里有一个标志)来解决该问题,在这种情况下,您不再需要通过eof来决定。

That would be a very good thing, because what you have now has a little twisted (and rather brittle!) logic -- which is precisely how this last-line-error came about, of course. 那将是一件非常好的事情,因为您现在拥有的逻辑有些扭曲(而且相当脆弱!)-当然,这正是最后一行错误的产生原因。

You would just need to change the match to include a new line if you don't chomp. 如果您不紧迫,则只需更改匹配项以包含新行。

while(<$fh>) {
  last if($_ eq "Team $team_number\n");
}

Chomp removes new line character from the end of the line as you are looping through each line of the file. 当您在文件的每一行中循环浏览时,Chomp将从行尾删除新的行字符。 Since you are matching the line with a text without new line, there is no match found and the file is read until the end. 由于您要使该行与没有换行符的文本匹配,因此找不到匹配项,并且将读取文件直到最后。 So the last condition to check End-Of-File is true and the "die" statement is executed. 因此,检查文件结束的最后一个条件为true,并执行了“ die”语句。

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

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