Perl 输出覆盖自身
我的代码循环遍历文件中的行,并尝试打印出每一行,并在开头和结尾添加一些内容。
但是,我得到这样的输出:“nominalte 奶牛”。
基本上,该行(标称)后面的位会覆盖它的开头。我知道删除 chomp 和 regex 行可以阻止这种效果,但我需要它在没有空格的一行上。我哪里出错了?
while ($line = <INPUT>) {
chomp $line;
$line =~ s/ //g;
printf "\@attribute %s nominal\n", $line;
}
I have code which loops through lines in a file, and tries to print out each line with something added at the start and end.
However, I get output like this: "nominalte cows".
Basically, the bit after the line (nominal) overwrites the start of it. I know that removing the chomp and regex lines stops this effect, but I need it to be on one line without spaces. Where am I going wrong?
while ($line = <INPUT>) {
chomp $line;
$line =~ s/ //g;
printf "\@attribute %s nominal\n", $line;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的输入文件可能来自 MS Windows,行尾编码为 CR-LF。您也可以仅
s/\r//
删除 CR。Your input file is probably from MS Windows with end of line encoded as CR-LF. You can also just
s/\r//
to remove the CR.您的变量中可能有
\r
。尝试使用\s
:请参阅 perlre 了解
的含义\s
。You might have
\r
in your variable. Try using\s
:See perlre for the meaning of
\s
.