note
rjt
<p>The following should be reasonably efficient. It does keep files open, one per unique DESCR element. Depending on how many DESCR elements you have in your real data, you may need to rethink this, possibly with a least-recently-used scheme.</p>
<code>
use warnings;
use strict;
my %fh_of; # Hash of filehandles
foreach my $file (<R*-*.txt>) {
open INPUT, "<$file" or die "Couldn't open $file: $!";
while (<INPUT>) {
my $fh;
if (/^DESCRP\s+(.+?)$/) {
my $des = $1;
unless (exists $fh_of{$des}) {
open $fh_of{$des}, ">>$des.txt"
or die "Couldn't open $des.txt: $!";
}
$fh = $fh_of{$des};
$file =~ /^(R.+?)-/; # Glob guarantees match
print $fh "$1$des\n";
next;
}
print $fh $_ if $fh;
}
close INPUT;
}
close $_ for (values %fh_of);
</code>
<p>With the input files as you've given them, I get the expected output. All errors are fatal; you might want to handle them more gracefully depending on your application. If an input file does not start with a DESCRP line, $fh will not be defined, so I just throw away records until I see a DESCRP line. Again, you may want to handle this differently.</p>
843691
843691