in reply to Search a file with ids from another file

Apart from the comments made above, I would suggest two things:

1) In your last while loop, you attempt to open and close a filehandle each time you come upon a match. This is highly inefficient. Open $OFILE before the loop, and close it after.

2) It would no doubt be better (or at least more elegant) to turn your @id_hits into a single regex, rather than looping through the array inside another loop. One simple way of doing this would be to add a line:

my $id_regex = join '|', @id_hits;

after the first while loop, but you could most likely create a more efficient regex if you used Regexp::Assemble

Putting that all together would give something like:

use strict; use warnings; use Regexp::Assemble; my $id_regex = Regexp::Assemble->new; my $IDFILE = ...; # Your filenames here open my $ifh, '<', $IDFILE or die "Could not open $IDFILE: $!"; while ( my $line = <$ifh> ) { $id_regex->add( substr $line, 0, 6 ); } close $ifh; open my $afh, '<', $AMPFILE or die "Could not open $AMPFILE: $!"; open my $ofh, '>', $OUTFILE or die "Could not open $OUTFILE: $!"; while ( my $line = <$afh> ) { chomp $line; if ( $line =~ $id_regex ) { my $next = <$afh>; print $ofh $line, "\t", $next if $next =~ /^Amp-commit/; } } close $ofh; close $afh;

Untested, unfortunately, as I don't have access to your data files...

Belated update (sorry!): changed

    my $next = <DATA>;

to

    my $next = <$afh>;