in reply to Re^3: How can you check to see if a file handle is already open?
in thread How can you check to see if a file handle is already open?

Limbic~Region++,

That thread was perfect! Thank you for pointing it out.

while(<INPUT>) { chomp $_; if ( $_ =~ m#^(\d+)/(\d+)/(\d+)\s+# ) { my $date = join( '', ( $3, $1, $2) ); if ( ! $file_handles{$date} ) { open($file_handles{$date}, ">>", "$output_dir/$date") +or die "Could not open $date for writing: $!\n"; print { $file_handles{$date} } "$_\n"; } elsif ( defined $file_handles{$date} ) { print { $file_handles{$date} } "$_\n"; } } }


agent_smith

Replies are listed 'Best First'.
Re^5: How can you check to see if a file handle is already open?
by alexm (Chaplain) on Jan 17, 2008 at 09:01 UTC
    This is quite the same technique that chaos_cat suggested before, just a bit more inefficient, since
    print { $file_handles{$date} } "$_\n";
    can be put outside the conditional opening of the file.