From: Gunnar Hjalmarsson <[EMAIL PROTECTED]> > Johnson, Reginald (GTI) wrote: > > In my code I am using grep successfully, > > But rather inefficient. A hash is a better tool to check for existence; > see the FAQ entry "perldoc -q contained". > > > but I would also like an > > output file that has the objects that don't match the grep. > > <snip> > > > while (<MHSFILE>) { > > chomp($_); > > push (@mhsArray, $_); > > } > > while ($line= <INFILE>) { > > chomp($line); > > print "$line\n"; > > @inmhs = grep/\b$line\b/i,@mhsArray; > > foreach $line (@inmhs) { > > print OUTFILE "$line\n"; > > } > > } #end while > > Consider this approach: > > my %mhsHash = map { chomp; $_ => 1 } <MHSFILE>; > while ( my $line = <INFILE> ) { > chomp $line; > print { $mhsHash{$line} ? *OUTFILE : *OUTFILE2 } "$line\n"; > }
That's not necessarily right. Actually it's most likely wrong. First the match in the grep{} was case insensitive. So you'd need something like my %mhsHash = map { chomp; lc($_) => 1 } <MHSFILE>; while ( my $line = <INFILE> ) { chomp $line; print { $mhsHash{lc $line} ? *OUTFILE : *OUTFILE2 } "$line\n"; } second the test did not check whether the $line equals to one of the items from MHSFILE, but whether any of those items match the line (treated as a regexp) between two word-breaks. Most likely not what the OP actually wanted to do. Which was it he wanted to do is hard to say. Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/