shaq
shaq

Reputation: 187

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

Upvotes: 1

Views: 2963

Answers (3)

Sergiy Ostrovsky
Sergiy Ostrovsky

Reputation: 2532

All you need is $hash{$ID} .= $line;. No if-elses. If there's no key $ID in the hash it would create one and concatenate $line to empty string, resulting exactly what you need.

Upvotes: 1

shawnhcorey
shawnhcorey

Reputation: 3601

You should store your data in a hash of arrays:

#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------

use charnames qw( :full :short   );
use English   qw( -no_match_vars );  # Avoids regex performance penalty

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

# conditional compile DEBUGging statements
# See http://lookatperl.blogspot.ca/2013/07/a-look-at-conditional-compiling-of.html
use constant DEBUG => $ENV{DEBUG};

# --------------------------------------

my %HoA = ();
while( my $line = <DATA> ){
  if( my ( $ID ) = $line =~ m{ ID \= ([^;]+) }msx ){
    push @{ $HoA{$ID} }, $line;
  }
}
print Dumper \%HoA;


__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

Upvotes: 0

Barmar
Barmar

Reputation: 781503

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

Upvotes: 1

Related Questions