我是Perl的初学者.我有一个文件两列.我想比较第一列(作为参考)和第二列(测试):
pppqqrrsttqrstrr pppqrrrsttqrstrr
if p in ref =~ p in test print p
if q in ref =~ q in test print q
if r in ref =~ r in test print r
if s in ref =~ s in test print s
if t in ref =~ t in test print W
if q in ref =~ r in test print w
Run Code Online (Sandbox Code Playgroud)
所以输出: pppqwrrsWWqrsWrr
我试过了:
#!/usr/bin/perl
use warnings;
use strict;
open my $F1, '>', 'match' or die $!;
while(<>){
chomp($_);
my @file = split ("\t| ",$_);
my @ref = split (//, $file[0]);
my @test = split (//, $file[1]);
for my $i (0 .. @ref -1) {
if(($ref[$i] =~ /Pp/) && ($test[$i] =~ /Pp/)){
print $F1 ("$ref[$i]");
}
elsif(($ref[$i] =~ /Qq/) && ($test[$i] =~ /Qq/)){
print $F1 ("$ref[$i]");
}
elsif(($ref[$i] =~ /Rr/) && ($test[$i] =~ /Rr/)){
print $F1 ("$ref[$i]");
}
elsif(($ref[$i] =~ /Ss/) && ($test[$i] =~ /Ss/)){
print $F1 ("$ref[$i]");
}
elsif(($ref[$i] =~ /Tt/) && ($test[$i] =~ /Tt/)){
print $F1 ("W");
}
elsif(($ref[$i] =~ m/Qq/) && ($test[$i] =~ m/Rr/)){
print $F1 ("w");
}
$i++;
}print $F1 ("\n");}
close $F1;
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何东西!
谢谢
看起来像这样:
$test[$i] =~ /Pp/
Run Code Online (Sandbox Code Playgroud)
您正在尝试使用其中一个P或其中的字符串查找字符串p,但您实际在做的是查找带有a P后跟a的字符串p.你打算做的是:
$test[$i] =~ /[Pp]/
Run Code Online (Sandbox Code Playgroud)
where [Pp]是一个与其中任何一个匹配的字符类.
但是,更好的方法是i在正则表达式上使用修饰符来使测试不区分大小写
$test[$i] =~ /p/i
Run Code Online (Sandbox Code Playgroud)