【发布时间】:2020-05-20 01:37:10
【问题描述】:
我有一个哈希数组,我用以下方式填充它:
# Array of hashes, for the files, regexps and more.
my @AoH;
push @AoH, { root => "msgFile", file => my $msgFile, filefh => my $msgFilefh, cleanregexp => s/.+Msg:/Msg:/g, storeregexp => '^Msg:' };
这是其中一个条目,我还有更多这样的。并且一直使用哈希的每个键值对来创建文件,从文本文件中清除行等等。问题是,我通过以下方式创建了文件:
# Creating folder for containing module files.
my $modulesdir = "$dir/temp";
# Creating and opening files by module.
for my $i ( 0 .. $#AoH )
{
# Generating the name of the file, and storing it in hash.
$AoH[$i]{file} = "$modulesdir/$AoH[$i]{root}.csv";
# Creating and opening the current file.
open ($AoH[$i]{filefh}, ">", $AoH[$i]{file}) or die "Unable to open file $AoH[$i]{file}\n";
print "$AoH[$i]{filefh} created\n";
}
但后来,当我尝试向文件描述符打印一行时,出现以下错误:
String found where operator expected at ExecTasks.pl line 222, near ""$AoH[$i]{filefh}" "$row\n""
(Missing operator before "$row\n"?)
syntax error at ExecTasks.pl line 222, near ""$AoH[$i]{filefh}" "$row\n""
Execution of ExecTasks.pl aborted due to compilation errors.
而且,这是我尝试打印到文件的方式:
# Opening each of the files.
foreach my $file(@files)
{
# Opening actual file.
open(my $fh, $file);
# Iterating through lines of file.
while (my $row = <$fh>)
{
# Removing any new line.
chomp $row;
# Iterating through the array of hashes for module info.
for my $i ( 0 .. $#AoH )
{
if ($row =~ m/$AoH[$i]{storeregexp}/)
{
print $AoH[$i]{filefh} "$row\n";
}
}
}
close($fh);
}
我尝试打印到文件的方式有什么问题?我尝试打印文件句柄的值,并且能够打印它。另外,我使用 storeregexp 成功打印了匹配项。
顺便说一句,我在一台装有 Windows 的机器上工作,使用 perl 5.14.2
【问题讨论】:
标签: perl filehandle