![](/img/trans.png)
[英]Is there anything in core Perl to auto-chomp lines from “<>” operator?
[英]Lines are not getting printed after application of chomp in perl
#!/usr/bin/perl
use warnings;
use Switch;
while (<>) {
next if /^\s*\/\//;
next if /^\s*$/;
my $line="$_";
chomp($line);
print $line;
print "Checking for line:\n$line";
}
这是我的代码,它从命令行获取输入文件。 它使用以下文件作为输入:
// Computes R0 = 2 + 3
@2
D=A
@3
D=D+A
@0
M=D
在代码中,我尝试通过chomp函数删除每行末尾的换行符。 但是当我尝试在应用chomp后打印该行时,它不会为该行打印任何内容。 它给出以下输出:
Checking for line: Checking for line: Checking for line: Checking for line: Checking for line: Checking for line:
为什么没有打印该行?
如果输入的行尾有\\r\\n
我可以重现您的问题。 chomp
将删除\\n
但保留\\r
(在Unix上),其作用是将光标移动到该行的开头(不前进到下一行)。 由于您的行很短,因此下一个输出( Checking for line
)将完全覆盖它。
对不起,鲍罗丁。 所以解决方案是:
使用比chomp
更硬的东西。 我一直喜欢用
$line =~ s/\\s+$//;
只要您不需要保留尾随空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.