# The first part is the same as the original. open IN, $Filename or die "Could not open file $!"; @array = ('file', 'this', 'dog', 'forward'); my %guard; READLOOP: while () { foreach $searchstring (@array) { if (m/$searchstring/i) { if (! exists $guard{$searchstring}) { push @stringsfound, $searchstring; last READLOOP if @stringsfound == @array; $guard{$searchstring} = 1; } } } } close IN; # a somewhat nicer output print join(" ",@stringsfound),"\n";