in reply to Nested foreach problem

This is a little better (but not tested):

NAME: foreach my $name ( @name_array ) { FILE: foreach my $file ( @files ) { open my $fh, '<', $file or die "Can't read '$file': $!"; while ( my $line = <$fh> ) { chomp $line; if ( lc $line eq lc $name ) { print OUTPUTFILE "$line\n"; next FILE; } } } }

Note a few things:

The way you're trying to do it with regular expressions means that you'll end up matching partial names with full names (i.e., "Fred" will match "Frederick" and "Liam" will match "William"). If that's what you want, fine. You can say $line =~ /\Q$wanted_name/ and get that (see also quotemeta).

If you want each name to be found only once regardless of how many files it's in, you can change my "next FILE" to "next NAME" to skip over other files once the name is found.

Update: I also Use strict and warnings