I have the below code. I am trying to find a match of 3 words in my log file and print the line if the match is found (only if all 3 words are present). It works fine if I hardcode the words (@typedefs) , but its not working if I have the same words in a array with strings (@typedefs_new). What is the mistake I am doing ?
my $str1="laa";
my $str2="faa";
my $str3="baa";
my @typedefs = qw(laa,faa,baa);
my @typedefs_new = ($str1,$str2,$str3);
my $file="/pathtofile/logfile.log";
open (FILE, $file) or die $!;
print "Output using typdefs_new array\n";
while ( my $line = <FILE> ) {
if ( any { $line =~ /$_/ } @typedefs_new ) {
print $line;
}
}
print "Output using typdefs array\n";
while ( my $line = <FILE> ) {
if ( any { $line =~ /$_/ } @typedefs ) {
print $line;
}
}
logfile.log:
laa ferg gerg faa rgrebf baa abc def
fber rgreg rgre greg bgbg rghgr grhr
Output:
Output using typedefs_new array
laa ferg gerg faa rgrebf baa abc def
fber rgreg rgre greg bgbg rghgr grhr
Output using typedefs array
laa ferg gerg faa rgrebf baa abc def
all, notany. You should also change your regex to something like/\b$_\b/to avoid matching words likeblaaandfaaa.