我在Perl中编写了以下代码。代码正在读取pdb文件并获取一些值。忽略代码的顶部,一切都在完美。 问题出现在子例程部分,我尝试将数组存储在hash3中,模型作为键的另一个关键位置 可以使用以下方法在if条件内访问数组值:
$ hash3 {$模型} {$坐标} [1]。
但是当我走出所有foreach循环并尝试访问元素时,我只获得一个值。 请查看结束foreach循环并告诉我访问哈希值的方法是错误的。
我正在使用的pdb文件可以从此链接http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=1NZS
下载#!/usr/bin/perl
open(IN,$ARGV[0]);
my @phosphosites;
my $model=1;
my %hash3;
while(<IN>)
{
#findmod(@line);
#finddist;
#findfreq;
if((/^MODRES/) && (/PHOSPHO/))
{
@line=split;
push(@phosphosites, $line[2]);
#print "$line[4]";
}
foreach $elements (@phosphosites){
if(/^HETATM\s+\d+\s+CA\s+$i/)
{
@line1=split;
#print "$line1[5]";
#print "$line1[6] $line1[7] $line1[8]\n";
push(@phosphositesnum, $line1[5]);
}
}
$pos=$line1[5];
#findspatial(\@line,\@line1);
}
my @ori_data=removeDuplicates(@phosphositesnum);
sub removeDuplicates {
my %seen = ();
my @vals = ();
foreach my $i (@_) {
unless ($seen{$i}) {
push @vals, $i;
$seen{$i} = 1;
}
}
return @vals;
}
$a=(@phosphosites);
print "$a\n";
print "@phosphosites\n";
print "@ori_data\n";
close(IN);
open(IN1,$ARGV[0]);
my (@data)=<IN1>;
spatial(\@ori_data);
sub spatial {
my @spatial_array1=@{$_[0]};
foreach $coordinates(@spatial_array1)
{
$model=1;
{foreach $data1(@data){
if($data1=~ m/^HETATM\s+\d+\s+CA\s+[A-Z]*\s+[A-Z]*\s+$coordinates/)
{
@cordivals=split(/\s+/,$data1);
push @{ $sphash{$model} },[$cordivals[6], $cordivals[7], $cordivals[8]];
$hash3{$model}{$coordinates}= \@cordivals;
#print "$model $coordinates $hash3{$model}{$coordinates}[6] $hash3{$model}{$coordinates}[7] $hash3{$model}{$coordinates}[8]\n";
#print "$model $sphash{$model}[$i][0] $sphash{$model}[$i][1] $sphash{$model}[$i][2]\n";
}
elsif($data1=~ m/^ENDMDL/)
{
$model++;
}
#print "$model $coordinates $hash3{$model}{$coordinates}[6] $hash3{$model}{$coordinates}[7] $hash3{$model}{$coordinates}[8]\n";
}
}
}
#foreach $z1 (sort keys %hash3)
# {
# foreach $z2(@spatial_array1){
# print "$z1 $z2";
# print "$hash3{$z1}{$z2}[6]\n";
# print "$z2\n";
# }
# }
}
使用Data :: Dumper选项后,它给了我这种输出
$VAR1 = {
'11' => {
'334' => [
'HETATM',
'115',
'CA',
'SEP',
'A',
'343',
'-0.201',
'-2.884',
'1.022',
'1.00',
'99.99',
'C'
],
'342' => $VAR1->{'11'}{'334'},
'338' => $VAR1->{'11'}{'334'},
'335' => $VAR1->{'11'}{'334'},
'340' => $VAR1->{'11'}{'334'},
'343' => $VAR1->{'11'}{'334'},
'336' => $VAR1->{'11'}{'334'}
},
'7' => {
'334' => $VAR1->{'11'}{'334'},
'342' => $VAR1->{'11'}{'334'},
'338' => $VAR1->{'11'}{'334'},
'335' => $VAR1->{'11'}{'334'},
'340' => $VAR1->{'11'}{'334'},
'343' => $VAR1->{'11'}{'334'},
'336' => $VAR1->{'11'}{'334'}
},
'2' => {
'334' => $VAR1->{'11'}{'334'},
'342' => $VAR1->{'11'}{'334'},
...
答案 0 :(得分:5)
变化:
@cordivals=split(/\s+/,$data1);
为:
my @cordivals=split(/\s+/,$data1);
似乎正在发生的是所有哈希元素都包含对同一个数组变量的引用,因为你没有将该变量设置为该迭代的局部变量。
通常,您应该对所有变量使用my
。