in reply to grep : partial and/or exact match
That code will be very slow, however, because it's recompiling the regex each time. Although you could avoid that problem with qr//, it would be simpler to skip grep and just make one big regex:my @restricted_words = qw ( funk shucks crud ); if (grep $word =~ /$_/, @restricted_words) { print "wash your mouth out!\n"; } else { print "Such a nice boy\n"; }
This approach will give you both speed and flexibility. Enjoy!my @restricted_words = qw ( funk shucks crud ); my $restricted_re = '(?:' . join('|', @restricted_words) . ')'; $restricted_re = qr/$restricted_re/; if ($word =~ $restricted_re) { print "wash your mouth out!\n"; } else { print "Such a nice boy\n"; }
|
|---|