in reply to Re^2: Only last record is written to the output file instead of all records
in thread Only last record is written to the output file instead of all records
Test for the condition prior to entering the loop, OR, open within the loop, but on each iteration check to see if the file is already open. Easy way to do that would be to use a lexical filehandle, scoped to just outside the loop. Here's an example:
{ my $fh; while ( ....condition.... ) { unless( defined( $fh ) ) { open $fh, '>', $filename or die $!; } # ....do your stuff... } close $fh or die $!; }
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Only last record is written to the output file instead of all records
by jwkrahn (Abbot) on Nov 17, 2008 at 09:30 UTC |