in reply to Multiple simultaneous filehandles

You're trying to use talk about the filehandle symbolically, that is, construct its name:

my $fh = "OUTPUT" . $i;

Don't do that. Since version 5.6, Perl allows you to conveniently put filhandles in lexically scoped variables. In your case you're indexing by a number, so an array of handles is probably the best choice.

# before the if ($. == 1) my @fhs; ... # open one open $fhs[$i], ">", $filename or die "Cannot open the output file: $!" +; ... # use it print {$fhs[$i]} "$line[0]\t$line[$i]\n";

Note it's important to control the scope of @fhs, so it's visible to your code that uses it. When it goes out of scope, the files are closed.

Update: fixed syntax error, thanks eyespoplikeamosquito++!

Replies are listed 'Best First'.
Re^2: Multiple simultaneous filehandles
by eyepopslikeamosquito (Archbishop) on Oct 24, 2006 at 06:42 UTC

    # use it print $fhs[$i] "$line[0]\t$line[$i]\n";
    That is a syntax error. Better style is:
    print {$fhs[$i]} "$line[0]\t$line[$i]\n";
    See Perl Best Practices, Chapter 10, "Printing to Filehandles".

    Update: Or, if you prefer OO-style IO:

    use IO::Handle; # ... $fhs[$i]->print("$line[0]\t$line[$i]\n");