open my $fh_input, "<", "input.txt" or die "$!"; my %fh = (); while (<$fh_input>) { chomp; my ($filename, $content) = split /\s+/, $_, 2; my $fh = undef; $fh = $fh{$filename} if defined $fh{$filename}; if (not defined $fh{$filename}) { open $fh, ">", "$filename.out" or die "$!"; $fh{$filename} = $fh; } else { $fh = $fh{$filename}; } print $fh $content, "\n"; } foreach (keys %fh) { close $fh{$_}; }