Perl 如果存在哈希键,则将新值添加到现有值

Perl 如果存在哈希键,则将新值添加到现有值,perl,hash,Perl,Hash,我有一个哈希结构,我想向现有值添加新值(而不是用新值更新)。 这是我的密码 use strict; use warnings; my %hash; while(<DATA>){ my $line=$_; my ($ID)=$line=~/ID=(.*?);/; #make a hash with ID as key

我有一个哈希结构,我想向现有值添加新值(而不是用新值更新)。
这是我的密码

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
使用严格;
使用警告;
我的%hash;
while(){
我的$line=$\ux;
我的($ID)=$line=~/ID=(.*);
#创建一个以ID为键的哈希           
如果(!exists$hash{$ID}){
$hash{$ID}=$line;
}
否则{
#将$line添加到现有值        
}
}
对于我的$key(key%散列){
打印$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

您应该将数据存储在数组散列中:

#!/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
#/usr/bin/env perl
严格使用;
使用警告;
# --------------------------------------
使用字符名qw(:full:short);
使用英语qw(-no_match_vars);#避免正则表达式性能惩罚
使用数据::转储程序;
#使Data::Dumper变得漂亮
$Data::Dumper::Sortkeys=1;
$Data::Dumper::Indent=1;
#设置Data::Dumper的最大深度,零表示无限制
本地$Data::Dumper::Maxdepth=0;
#条件编译调试语句
#看http://lookatperl.blogspot.ca/2013/07/a-look-at-conditional-compiling-of.html
使用常量DEBUG=>$ENV{DEBUG};
# --------------------------------------
我的%HoA=();
while(我的$line=){
如果(my($ID)=$line=~m{ID\=([^;]+)}msx){
推送{$HoA{$ID}},$行;
}
}
打印转储程序\%HoA;
__资料__
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

您只需要
$hash{$ID}.=$line。如果埃尔斯没有。

如果散列中没有键
$ID
,它将创建一个键,并将
$line
连接到空字符串,得到您需要的结果。

欢迎使用StackOverflow。请阅读以下内容并改进您的问题:首先,请告诉我们您尝试了什么,以及为什么没有成功。
#!/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