另一个哈希包含名称作为键(由_分割)并且有4个部分(我比较前三个部分)。该值是一个字符串,其长度需要比较,如果名称相同并且已经存在于散列(前三部分)中,我需要保持字符串的长度最长,如果长度相等,我不会关心它保留哪一个。我知道迭代它会在删除键值对时产生问题。
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";
}
}
}
答案 0 :(得分:1)
您的代码非常复杂,但我认为下面的示例代码可以满足您的需求。它的工作原理是将%anotherhash
的内容(请不要对变量使用这些可怕的无意义名称)一次复制到%temphash
一个元素,并且只有在新的%anotherhash
的值超过%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;
}
}