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 ))
??