http://www.perlmonks.org?node_id=705275


in reply to Nested foreach problem

You don't seem to have problem 1 in your code. As for problem 2, if a name matches in several places, all occurrences will be printed. If you want uniqueness use an hash. The following example illustrates the differences:

use strict; use warnings; my @names = ('joe', 'mary', 'francis'); my @lines = <DATA>; my %lines_matched; my @all_found; foreach my $name (@names) { my $wanted = $name; foreach my $line (@lines) { if ($line =~ m/\b$wanted\b/i) { push @all_found, $line; $lines_matched{$line} = 1; } } } print foreach @all_found; print "\nNow the uniques:\n"; print foreach keys %lines_matched; __DATA__ Joe is lazy Robin Wood Mary Poppins Francis Bacon Ruppert Joe married Mary

outputs

Joe is lazy Joe married Mary Mary Poppins Joe married Mary Francis Bacon Now the uniques: Francis Bacon Joe married Mary Joe is lazy Mary Poppins

update. added \b after reading kyle's comment.