在perl中创建输出文件后删除空白的问题
我已经编写了一个脚本,将输出保存在一个Perl脚本中,但出于某种原因,它在每一行的末尾留下了空间。我尝试使用Perl正则表达式,但它不起作用。有人能看看我的代码,让我知道我做错了什么吗 我的代码在perl中创建输出文件后删除空白的问题,perl,Perl,我已经编写了一个脚本,将输出保存在一个Perl脚本中,但出于某种原因,它在每一行的末尾留下了空间。我尝试使用Perl正则表达式,但它不起作用。有人能看看我的代码,让我知道我做错了什么吗 我的代码 open FILE, ">", "finaloutput.txt" || die "cannot create"; my @output = ``; # (here i am using back ticks to run third party command) foreach my $
open FILE, ">", "finaloutput.txt" || die "cannot create";
my @output = ``; # (here i am using back ticks to run third party command)
foreach my $output (@output) {
chomp $output;
my $remove_whitespace = $output;
$remove_whitespace =~ s/^\s+|\s+$//g;
print FILE "$remove_whitespace \n";
}
close FILE;
即使这样做了,也会在输出的每一行末尾留下一个空白。请引导我
谢谢。出于某种原因,您在打印对账单的末尾添加了多个空格。将打印对账单更改为:
print FILE "$remove_whitespace\n";
此外,您不应该再使用全局样式文件句柄。相反,请使用以下方法:
open my $file, '>', "output.txt";
print $file "Some string\n";
close $file;
当您打印文件$remove\u空白\n;在每行末尾添加2个空格,打印文件$remove\u whitespace\n;相反您在每行末尾添加了两个空格:
print FILE "$remove_whitespace \n";
^^
||
扔掉那些!解决方案:
print FILE "$remove_whitespace\n";
-or-
print FILE $remove_whitespace, "\n";
当您打印文件$remove\u空白\n;如果在每行末尾添加2个空格,请打印文件$remove\u whitespace\n;`instead@mirod回答这个问题,这样我们就可以投票表决了。