shaq shaq - 20 days ago 8
Perl Question

if exist a hash key add the new value to existing value

I have a hash structure and I want to add new value to the existing value (not update with new value ).

here is my code.

use strict;
use warnings;
my %hash;
while(<DATA>){
my $line=$_;
my ($ID)=$line=~/ID=(.*?);/;
#make a hash with ID as key
if (!exists $hash{$ID}){
$hash{$ID}= $line;
}
else{
#add $line to the existing value
}
}
for my $key(keys %hash){
print $key.":".$hash{$key}."\n";
}
__DATA__
ID=13_76; gi|386755343
ID=13_75; gi|383750074
ID=13_75; gi|208434224
ID=13_76; gi|410023515
ID=13_77; gi|499086767

Answer
    else{
       $hash{$ID} .= $line;                                                                                                                                                                                                                                                                                                                                  
    }