Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在perl中创建输出文件后删除空白的问题_Perl - Fatal编程技术网

在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 $

我已经编写了一个脚本,将输出保存在一个Perl脚本中,但出于某种原因,它在每一行的末尾留下了空间。我尝试使用Perl正则表达式,但它不起作用。有人能看看我的代码,让我知道我做错了什么吗

我的代码

 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回答这个问题,这样我们就可以投票表决了。