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 ?
我有以下代码。我试图在我的日志文件中找到3个单词的匹配,如果找到匹配则打印该行(仅当所有3个单词都存在时)。如果我对单词进行硬编码(@typedefs),它可以正常工作,但如果我在带有字符串的数组中有相同的单词(@typedefs_new),它就无法正常工作。我在做什么错?
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:
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
1 个解决方案
#1
8
qw()
separates words by whitespace, not by comma. So, your code is equivalent to
qw()按空格分隔单词,而不是用逗号分隔单词。所以,你的代码相当于
my @typedefs = ( 'laa,faa,baa' );
warnings should have told you:
警告应该告诉你:
Possible attempt to separate words with commas
#1
8
qw()
separates words by whitespace, not by comma. So, your code is equivalent to
qw()按空格分隔单词,而不是用逗号分隔单词。所以,你的代码相当于
my @typedefs = ( 'laa,faa,baa' );
warnings should have told you:
警告应该告诉你:
Possible attempt to separate words with commas