in reply to Match a string only once in a file
The foreach block can also be written aslocal $/; my $file = <IN>; close IN; @array=('file', 'this', 'dog', 'forward'); foreach $searchstring(@array) { if ($file=~m/$searchstring/i) { push @stringsfound,$searchstring; } }
I changed your regular expression, omitting the (.*)? since it didn't seem to be fulfilling any useful function (ordinarily this would capture any characters before $searchstring into $1, but you're not using $1 for anything).@stringsfound=grep {$file=~/$_/i} @array;
|
|---|