【发布时间】:2021-01-08 04:17:07
【问题描述】:
我想将所有服务器值写入文本文件。但是我的输出文本文件只能写最后一个值。例如,$theServer 值是
as1tp.com
as2tp.com
as3tp.com
as4tp.com
as5tp.com
我只能在我的文本文件中写入最后一个值as5tp.com,而不是在输出文本文件中写入所有这些服务器值。下面是我的代码。如何将所有值写入tier1.txt 文件?
use strict;
use warnings;
my $outputfile= "tier1.txt"
my $theServer;
foreach my $theServernameInfo (@theResult){
$theServer = $theServernameInfo->[0];
print "$theServer\n";
open(my $fh, '>', $outputfile) or die "Could not open file '$outputfile' $!";
print $fh "$theServer";
close $fh;
}
【问题讨论】:
-
将打开和关闭移出循环
-
@ikegami 我试图将 open 和 close 语句移到循环之外,也只看到最后一个 rerver 名称:(
-
第三行缺少分号(
;)。 -
您能否告诉我们
Dumper数组的Dumper值是什么?