Array of file descriptors

I wanted to choose what data should be inserted into the file depending on the index. However, I seem to stick with the following.

I created files using an array of file descriptors:

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";

However, for some reason, I get an error message on the last line. Help someone ...?

+5
source share
3 answers

It should be simple:

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";
+17
source

You can always use object oriented syntax:

$file_h[$file]->print("$record_r1[0]$record_r1[1]$record_r1[2]$record_r1[3]\n");

Alternatively, you can print the array more simply:

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

Or so, if these four elements are actually all:

$file_h[$file]->print("@record_r1\n");
+4
source

First try assigning a $file_h[$file]temporary variable:

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";

As far as I remember, Perl does not recognize it as an output descriptor otherwise, complaining of invalid syntax.

+1
source

All Articles