我编写了一些代码,可以在 3 个不同的 HoA 中找到重叠的键,其中包含一些我稍后对它们进行排序的信息:
#!/usr/bin/perl
use warnings;
use strict;
my @intersect;
for my $key (sort keys %hash1) {
if (exists $hash2{$key} && $hash3{$key} ) {
my ($hit1, $percent_id1) = @{ $hash1{$key}[-1] };
my ($hit2, $percent_id2) = @{ $hash2{$key}[-1] };
my ($hit3, $percent_id3) = @{ $hash3{$key}[-1] };
push @intersect, "$key\tC1:[$condition1]$hit1 [$percent_id1]\tC2:[$condition2]$hit2 [$percent_id2]\tC3:[$condition3]$hit3 [$percent_id3]\n\n";\n";
}
}
我正在尝试调整脚本以找到存在于以下位置的键:
- hash1 和 hash2,但不是 hash3
- hash2 和 hash3,但不是 hash1
- hash1 和 hash3,但不是 hash2
我正在使用的(例如第一个实例):
elsif (exists $hash2{$key} && !exists $hash3{$key} ) { # Is this the right way to specify a 'not exists'?
my ($hit1, $percent_id1) = @{ $blast1{$key}[-1] };
my ($hit2, $percent_id2) = @{ $blast2{$key}[-1] };
push @intersect, "$key\tC1:[$condition1]$hit1 [$percent_id1]\tC2:[$condition2]$hit2 [$percent_id2]\n";
}
稍后在代码中,我循环遍历每个@intersect
以便对它们进行排名(下面发生的事情的细节在很大程度上无关紧要):
foreach (@intersect) {
chomp;
my (@condition1_match) = ($_ =~ /C1:.*?Change:(-?\d+\.\d+|-?inf)/);
@q_value1 = ($_ =~ /C1:.*?q:(\d+\.\d+)/);
my (@percent_id) = ($_ =~ /C\d+:.*\[(\d+\.\d+)\]/);
push @percentages, "@percent_id%";
my (@condition2_match) = ($_ =~ /C2:.*?Change:(-?\d+\.\d+|-?inf)/);
@q_value2 = ($_ =~ /C2:.*?q:(\d+\.\d+)/);
my (@condition3_match) = ($_ =~ /C3:.*?Change:(-?\d+\.\d+|-?inf)/);
@q_value3 = ($_ =~ /C3:.*?q:(\d+\.\d+)/);
my $condition1_match = $condition1_match[0] // $condition1_match[1];
my $condition2_match = $condition2_match[0] // $condition2_match[1];
my $condition3_match = $condition3_match[0] // $condition3_match[1];
if (abs $condition1_match > abs $condition2_match && abs $condition1_match > abs $condition3_match) {
push @largest_change, $condition1_match;
}
elsif (abs $condition2_match > abs $condition1_match && abs $condition2_match > abs $condition3_match) {
push @largest_change, $condition2_match;
}
else { push @largest_change, $condition3_match}
显然,在一个键存在于两个而不是三个哈希中的情况下,会有很多变量是 undef 的实例,因此我得到了很多Use of uninitialized value in...
我应该在每个变量前面加上if (defined ($variable ))
??