Problem in perl program in printing all the entries

Hi all,

I have a perl program where it checks for the presence of a particular line and prints the record if that line is present. But in the output file, all the entries are not being printed even if the entry has that line. The line to be matched is *FIELD* AV. The program which I wrote is given below. I have no idea where I might be wrong. Any suggestions are welcome. Thanks in advance.

#!/usr/bin/perl
$/="\n\n";
open(DATA,'mutationsonly.txt');
# or die("Could not open mutationsonly file.");
open(MYOUTPUT, ">newmutations.txt");
# or die ("could not open newmutations file.");
my @records= <DATA>;
close(MYOUTPUT);
close(DATA);
foreach my $record (@records)
{
chomp;
my $find=/^\*FIELD\*\sAV$/;
if ($record=~m/ $find /) 
{
print "$record";
}
}

For readability, here's a formatted and numbered version:

  1 #!/usr/bin/perl
  2 $/ = "\n\n";
  3 open( DATA, 'mutationsonly.txt' );
  4
  5 # or die("Could not open mutationsonly file.");
  6 open( MYOUTPUT, ">newmutations.txt" );
  7
  8 # or die ("could not open newmutations file.");
  9 my @records = <DATA>;
 10 close(MYOUTPUT);
 11 close(DATA);
 12 foreach my $record (@records) {
 13     chomp;
 14     my $find = /^\*FIELD\*\sAV$/;
 15     if ( $record =~ m/ $find / ) {
 16         print "$record";
 17     }
 18 }

My guess is that in line 14 you're trying to assign a regular expression to a variable, and use that in the match below. To do that, you'd have to use the qr// construct, and it would be much more efficient to do that outside the loop, instead of each time again.

Other than that, you can probably do the same thing with a simple grep {} , eg

#!/usr/bin/perl
#$/ = "\n\n";
open( DATA, 'mutationsonly.txt' );

# or die("Could not open mutationsonly file.");
open( MYOUTPUT, ">newmutations.txt" );

# or die ("could not open newmutations file.");
my @records = <DATA>;
close(MYOUTPUT);
close(DATA);
print grep { /^\*FIELD\*\sAV$/ } @records;