问题描述
我写了一些代码,可以在3个不同的HoA中找到重叠的键,这些键包含我稍后对它们进行排序的一些信息:
#!/ usr / bin / perl
使用警告;
使用strict;
my @intersect;
for $ key(sort key%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] \\\
\\\
; \\\
;
}
}
我试图修改脚本以查找存在于以下位置的键:
- hash1和hash2,但不是hash3
- hash2和hash3,但不是hash1
- hash1和hash3,但不是hash2
我正在使用它(例如,用于第一个实例):
elsif(exists $ hash2 {$ key}&&!exists $ hash3 {$ key}){#这是指定'不存在'的正确方法吗?
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;
}
稍后在代码中循环t通过每个 @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+)\]/);
推送@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]; (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的实例,因此我得到了很多在...中使用未初始化的值
$ b
我应该在每个变量前加上 if(defined($ variable))
??
my%seen; (%hash1),键(%hash2),键(%hash3);
++ $见过{$ _}
for(keys(%seen)){
next if $ seen {$ _}!= 2;
print($ _被发现在两个哈希中\\\
);
}
此版本可追踪钥匙来自何处:
my%seen;
为键(%hash1)推送@ {$ seen {$ _}},'hash1';
为键(%hash2)推送@ {$ seen {$ _}},'hash2';
为键(%hash3)推送@ {$ seen {$ _}},'hash3';
for(keys(%seen)){
next if @ {$ seen {$ _}}!= 2;
print($ _ found in @ {$ seen {$ _}} \\\
);
}
I've written some code that finds overlapping keys in 3 different HoAs that contain some information on which I sort them later:
#!/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";
}
}
I'm trying to adapt the script to also find keys that exist in:
- hash1 and hash2, but not hash3
- hash2 and hash3, but not hash1
- hash1 and hash3, but not hash2
For which I'm using (e.g. for the first instance):
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";
}
Later in the code I loop through each @intersect
in order to rank them (the details of what's going on below are largely irrelevant):
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}
Obviously in the case where a key exists in two, but not three hashes, there will be a lot of instances where variables are undef, and as such I get a lot of Use of uninitialized value in...
Should I be prefixing each variable with if (defined ($variable ))
??
my %seen;
++$seen{$_} for keys(%hash1), keys(%hash2), keys(%hash3);
for (keys(%seen)) {
next if $seen{$_} != 2;
print("$_ is found in exactly two hashes\n");
}
This version tracks where the keys came from:
my %seen;
push @{ $seen{$_} }, 'hash1' for keys(%hash1);
push @{ $seen{$_} }, 'hash2' for keys(%hash2);
push @{ $seen{$_} }, 'hash3' for keys(%hash3);
for (keys(%seen)) {
next if @{ $seen{$_} } != 2;
print("$_ found in @{ $seen{$_} }\n");
}
这篇关于'做些什么'如果密钥存在于hash1中而不存在于hash2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!