我建了一个数组,比如A = [a1,a2,...aN]。如何将此数组保存到数据文件中,每个元素都放在一行中。换句话说,对于数组 A,文件应该是这样的
a1
a2
a3
...
最佳答案
非常简单(当然,这是假设您的数组被明确指定为数组数据结构,而您的问题并没有说清楚):
#!/usr/bin/perl -w
use strict;
my @a = (1, 2, 3); # The array we want to save
# Open a file named "output.txt"; die if there's an error
open my $fh, '>', "output.txt" or die "Cannot open output.txt: $!";
# Loop over the array
foreach (@a)
{
print $fh "$_\n"; # Print each entry in our array to the file
}
close $fh; # Not necessary, but nice to do
上面的脚本会将以下内容写入“output.txt”:
1
2
3
关于perl - 将数组保存到纯文本文件中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8966852/