文件句柄数组

A14*_*A14 5 perl

我想根据索引选择要放入哪个文件的数据.但是,我似乎陷入了以下困境.

我使用文件句柄数组创建了文件:

my @file_h;
my $file;
foreach $file (0..11)
{
    $file_h[$file]= new IT::File ">seq.$file.fastq";
}

$file= index;
print $file_h[$file] "$record_r1[0]$record_r1[1]$record_r1[2]$record_r1[3]\n";
Run Code Online (Sandbox Code Playgroud)

但是,我在最后一行出于某种原因出错了.帮助任何人......?

tch*_*ist 18

那应该只是:

my @file_h;
for my $file (0..11) {
    open($file_h[$file], ">", "seq.$file.fastq")
       || die "cannot open seq.$file.fastq: $!";
}

# then later load up $some_index and then print 
print { $file_h[$some_index] } @record_r1[0..3], "\n";
Run Code Online (Sandbox Code Playgroud)


Mar*_*eed 5

您始终可以使用面向对象的语法:

$file_h[$file]->print("$record_r1[0]$record_r1[1]$record_r1[2]$record_r1[3]\n");
Run Code Online (Sandbox Code Playgroud)

此外,您可以更简单地打印出阵列:

$file_h[$file]->print(@record_r1[0..3],"\n");
Run Code Online (Sandbox Code Playgroud)

或者像这样,如果这四个元素实际上是整个事物:

$file_h[$file]->print("@record_r1\n");
Run Code Online (Sandbox Code Playgroud)


mjh*_*nig 1

首先尝试将 分配$file_h[$file]给临时变量:

my @file_h;
my $file;
my $current_file;

foreach $file (0..11)
{
    $file_h[$file]= new IT::File ">seq.$file.fastq";
}

$file= index;
$current_file = $file_h[$file];

print $current_file "$record_r1[0]$record_r1[1]$record_r1[2]$record_r1[3]\n";
Run Code Online (Sandbox Code Playgroud)

据我所知,Perl 不会将其识别为输出句柄,否则会抱怨语法无效。