'Do something' if key exists in hash1 and

2019-06-14 13:47发布

问题:

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

回答1:

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");
}