use strict; ... my $found = 0; if ( $input ) { foreach my $image ( @files ) { # are you just looking to see if image CONTAINS input? if ( $image =~ /$input/ ) { $found = 1; last; } } } print "$input ".($found ? "found" : "not found")."\n";