为什么第二个正则表达式“ grep(/ keyword /,@ array”)在Perl中不能使用?

时间:2019-01-14 14:28:45

标签: regex perl grep

我编写了一个Perl程序来分析我的研究数据。
我的Perl脚本的一个功能是用来计算不同组中的原子数(我使用了两个数组# Make a data.frame with 10 points and the aesthetics associated wit each D_legend = data.frame( x = 1:10, y = rnorm(10), lwd = factor(c(2,2,2,2,2,2,1,1,1,1)), lty = factor(c(1,1,1,2,2,2,1,1,1,1)), color=c(rep('black', 6), rep('gray', 4)) ) # Plot it. I want a nice legend here! ggplot(D_legend, aes(x=x, y=y, lty=lty, lwd=lwd, color=color)) + geom_line() + theme(legend.position="top") @former_lists来识别两组)。

如果原子名称在组1(@modifier_lists)中,则变量@former_lists;
如果它在组2($cnt_former_intf++)中,则变量@modifier_lists;
如果是氧原子,则$cnt_modf_intf++;
其他{{$cnt_oxyg_intf++}。

下面是我的代码的一部分。

$cnt_other_intf++

结果如下所示。

......
my $flg_interface;
my @former_lists;
my $cnt_former_intf=0;
my $cnt_former_exbox=0;
my $cnt_modf_intf=0;
my $cnt_modf_exbox=0;
my $cnt_oxyg_intf=0;
my $cnt_oxyg_exbox=0;
my $cnt_other_intf=0;
my $cnt_other_exbox=0;
$former_lists[0]='SI';$former_lists[1]='AL';
my @modifier_lists;
$modifier_lists[0]='CA';$modifier_lists[1]='NA';
my $hash_key;
my %hash_type_spc;
$hash_type_spc{1}='SI';
$hash_type_spc{2}='AL';
$hash_type_spc{3}='CA';
$hash_type_spc{4}='O';
$hash_type_spc{5}='H';
$hash_type_spc{6}='NA';
my @atom_type;
$atom_type[1]=1;
$atom_type[2]=2;
$atom_type[3]=3;
$atom_type[4]=4;
$atom_type[5]=5;
$atom_type[6]=6;
my $atom_id;

for($atom_id=1;$atom_id<=17587;$atom_id++)
{  $hash_key=$atom_type[$atom_id];
  $_=uc($hash_type_spc{$hash_key});chomp($_);
  if ($flg_interface ==1)   #atom is in interface box
  {
    if($_ eq 'O'){$cnt_oxyg_intf++;}
    elsif($_ eq 'H'){$cnt_hydg_intf++;}
    elsif(grep(/$_/,@former_lists)  eq 1){$cnt_former_intf++;}
    #elsif(grep(/$_/,@modifier_lists) == 1){$cnt_modf_intf++;}
    elsif(grep(/$_/,@modifier_lists) eq 1){$cnt_modf_intf++;}
    else{$cnt_other_intf++;}
  }
  else                      #atom is in extended box
  {
    if($_ eq "O"){$cnt_oxyg_exbox++;}
    elsif($_ eq "H"){$cnt_hydg_exbox++;}
    elsif(grep(/$_/,@former_lists) eq 1){$cnt_former_exbox++;}
    elsif(grep(/$_/,@modifier_lists) eq 1){$cnt_modf_exbox++;}
    else{$cnt_other_exbox++;}
  }
}#end for
print "1021 $_$atom_id \t\$flg_interface=$flg_interface \t\$cnt_former_intf=$cnt_former_intf \t\$cnt_modf_intf=$cnt_modf_intf \t\$cnt_modf_intf=$cnt_modf_intf\t\$cnt_former_exbox=$cnt_former_exbox\t\$cnt_modf_exbox=$cnt_modf_exbox\n";
$tmp=<STDIN>; 

....

其中1021是标签。在这里

第一个输出SI6090应该具有1021 SI6090 $flg_interface=0 $cnt_former_intf=0 $cnt_modf_intf=0 $cnt_former_exbox=0 $cnt_modf_exbox=1 1021 AL7235 $flg_interface=0 $cnt_former_intf=0 $cnt_modf_intf=0 $cnt_former_exbox=0 $cnt_modf_exbox=2 1021 CA8029 $flg_interface=0 $cnt_former_intf=0 $cnt_modf_intf=0 $cnt_former_exbox=0 $cnt_modf_exbox=3 而不是$cnt_former_exbox=1;
第二个输出AL7235应该具有0而不是$cnt_former_exbox=2;
第三输出CA8029应该具有0而不是$cnt_modf_exbox=1

任何建议和帮助将不胜感激。
如果您可以分享更有效的方式,我将非常感激。

注意:我的数据很重。我必须考虑运行效率。

1 个答案:

答案 0 :(得分:1)

感谢大家的时间和帮助。

在线搜索后,我发现正则表达式不正确。如果要搜索数组是否包含元素,则需要使用

            $tmp=uc($hash_type_spc{$hash_key});chomp($tmp);
            if($tmp eq 'O'){$cnt_oxyg_intf++;}
            elsif($tmp eq 'H'){$cnt_hydg_intf++;}
            #elsif($tmp eq "CA"){$cnt_modf_intf++;}
            elsif(grep { $tmp eq $_ } @former_lists){$cnt_former_intf++;}
            elsif(grep { $tmp eq $_ } @modifer_lists){$cnt_modf_intf++;}
            else{$cnt_other_intf++;}

在这里,我将$ _ = uc($ hash_type_spc {$ hash_key}); chomp($ _)修改为$ tmp = ...

如果将regex表达式更改为上述表达式,我可以获得正确的结果。但是,我不完全了解此正则表达式。任何解释将不胜感激。