Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较perl中的两个字符串以查找不匹配项_Perl - Fatal编程技术网

比较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

我是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
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