Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File handle array

Tags:

perl

I wanted to choose what data to put into which file depending on the index. However, I seem to be stuck with the following.

I have created the files using an array of file handles:

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, I get an error for some reason in the last line. Help anyone....?

like image 626
A14 Avatar asked Jun 03 '12 01:06

A14


2 Answers

That should simply be:

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";
like image 137
tchrist Avatar answered Nov 15 '22 06:11

tchrist


You can always use the object-oriented syntax:

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

Also, you can print out the array more simply:

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

Or like this, if those four elements are actually the whole thing:

$file_h[$file]->print("@record_r1\n");
like image 20
Mark Reed Avatar answered Nov 15 '22 06:11

Mark Reed