use strict; use warnings; my @array = ("x y.g z 123", "a b.f c 456","a b.f c 456" ); #added lots more on the runs through smallprof. my @include = ("b","q"); my @keep; my %testHash = map { $_,1} @include; for my $tmp (@array){ my @tokens = split /\s+/,$tmp; my ($test,$rubbish) = split /\./, $tokens[1],2; if (exists $testHash{$test}) { push (@keep, $tmp); }; } print keys %testHash , "\n"; print "@keep";