比较perl中的两个字符串以查找不匹配项
我是Perl的初学者。我有一个两列的文件。我想将第一列作为参考与第二列测试进行比较:比较perl中的两个字符串以查找不匹配项,perl,Perl,我是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
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
so输出: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;
但我什么都没得到
谢谢你它看起来像这样:
$test[$i] =~ /Pp/
你试图找到一个包含p或p的字符串,但实际上你要做的是找到一个p后跟一个p的字符串。你想做的是:
$test[$i] =~ /[Pp]/
其中,[Pp]是一个字符类,它将与其中任何一个匹配
然而,更好的方法是在正则表达式上使用i修饰符,使测试不区分大小写
$test[$i] =~ /p/i