shaq shaq - 8 months ago 49
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;
my $line=$_;
my ($ID)=$line=~/ID=(.*?);/;
#make a hash with ID as key
if (!exists $hash{$ID}){
$hash{$ID}= $line;
#add $line to the existing value
for my $key(keys %hash){
print $key.":".$hash{$key}."\n";
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 Source
       $hash{$ID} .= $line;