my %match = map { $_ => 1 } qw(GCGAT CACGT); while (<>) { my $line = $_; s/N//; # remove the first N in the string print "match: $line" if $match{substr($_, 0, 5)} }