Problems with delete in hash perl
-
21-12-2019 - |
Question
Another hash contains name as key (split by _) and has 4 parts(I am comparing the first three parts). The value is a string whose length i need to compare and If the name is same and already exists in the hash (the first three parts), I need to keep the string with longest length and if the lengths are equal, I don't care which one it keeps. I know with iteration it is creating issues as the key-value pairs are being deleted.
my %temphash=();
%temphash=%anotherhash;
foreach my $tempkey(keys %temphash){
my @tempkey_splitted = split /\_/, $tempkey;
my $tempkey_newfamily = $tempkey_splitted[0];
my $tempkey_newgenera = $tempkey_splitted[1];
my $tempkey_newspecies = $tempkey_splitted[2];
my $tempkey_catstring ="$tempkey_newfamily"."_$tempkey_newgenera"."_$tempkey_newspecies";
my $sequence_realkey="";
my $sequence_tempkey="";
my $length_realkey="";
my $length_tempkey="";
if ($realkey_catstring eq $tempkey_catstring){
$sequence_realkey = $anotherhash{$realkey};
$length_realkey = length($sequence_realkey);
#print "$anotherhash{$realkey}";
#print "$length_realkey";
$sequence_tempkey = $temphash{$tempkey};
#print "$anotherhash{$tempkey}";
$length_tempkey = length($sequence_tempkey);
# print "$length_tempkey";
if($length_realkey>$length_tempkey){
delete($temphash{$tempkey});
#print ">$realkey\n$anotherhash{$realkey}\n\n";
}
elsif($length_tempkey>$length_realkey){
delete($temphash{$realkey});
#print ">$tempkey\n$anotherhash{$tempkey}\n\n";
}
elsif($length_tempkey eq $length_realkey){
delete($temphash{$realkey});
}
}
else{
print "do nothing";
}
}
}
Solution
Your code is quite convoluted, but I think the example code below does what you need. It works by copying the contents of %anotherhash
(please don't use such dreadful meaningless names for your variables) to %temphash
one element at a time, and only if the new value from %anotherhash
is longer than the %temphash
my %temphash;
while (my ($key, $val) = each %anotherhash) {
my @key = split /_/, $key;
my $tempkey = join '_', @key[0,1,2];
my $tempval = $temphash{$tempkey};
unless (defined $tempval and length $tempval >= length $val) {
$temphash{$tempkey} = $val;
}
}