如何从子例程返回此哈希值?

时间:2017-04-24 21:21:23

标签: perl hash

我有一个子程序,可以生成输入序列的d邻居,我在here的帮助下获得了帮助。我的子程序看起来像:

#subroutine for generating d-neighbors
    sub generate_d_neighbors{
        # $sequence is the sequence to generate d-neighbors from
        my ($sequence) = @_;
        my @temp;
        my %returnHash;
        my @tempSeq = @$sequence;
        for(my $i = 0; $i <= 3; $i++){
            #print "-------------------\n";
            #reset back to original sequence
            @$sequence = @tempSeq;
            my @l = qw(A T G C); #list of possible bases
            my $baseToRemove = @$sequence[$i]; #get base from sequence at current index
            @temp = grep {$_ ne $baseToRemove} @l; #remove base

            for (my $j = 0; $j <= 2; $j++){         
                #replace sequence[i] with temp[j]
                @$sequence[$i] = $temp[$j];
                #add sequences to hash
                $returnHash{@$sequence} = $i;
            }
        }
        return %returnHash;
    }

我想返回序列的哈希值。但是,当我用

进行测试时
my @testSeq = ("A","T","C","G");
my %neighborhood = generate_d_neighbors(\@testSeq);
for my $neighbor (keys %neighborhood){
  print "key: $neighbor\t value: $neighborhood{$neighbor}\n";
}

我得到的输出 key:4 value:3

我希望输出是生成的序列作为键(因为它们不能有重复),并且序列中的起始位置作为值。我做错了什么?

0 个答案:

没有答案