Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: pushing multiple key-value pairs into an existing hash

by johngg (Abbot)
on Feb 12, 2013 at 00:38 UTC ( #1018271=note: print w/ replies, xml ) Need Help??


in reply to pushing multiple key-value pairs into an existing hash

Adding the key/value pairs one at a time, as shown by Kenosis, is the most obvious and straightforward way to do this. However, you could process all of your new data into separate key and value arrays using split and map before updating your %gene hash in one go via a slice.

$ perl -Mstrict -Mwarnings -MData::Dumper -e ' > my %gene = ( > NCRNA00115 => 79854, > SAMD11 => 148398, > N0C2l => 26155, > PLEKHN1 => 84069, > HES4 => 57801, > ); > print Data::Dumper->Dumpxs( [ \ %gene ], [ qw{ *gene } ] ); > > open my $newFH, q{<}, \ <<EOD or die qq{open: < HEREDOC: > $!\n}; > ATAD3B 83858 > ATAD3A 55210 > SSU72 29101 > SLC35E2 9906 > GNB1 2782 > TMEM52 339456 > EOD > > my( @newKeys, @newValues ); > my $iter = 0; > push @{ ++ $iter % 2 ? \ @newKeys : \ @newValues }, $_ for > map { split } > <$newFH>; > > @gene{ @newKeys } = @newValues; > print Data::Dumper->Dumpxs( [ \ %gene ], [ qw{ *gene } ] );' %gene = ( 'HES4' => 57801, 'N0C2l' => 26155, 'PLEKHN1' => 84069, 'NCRNA00115' => 79854, 'SAMD11' => 148398 ); %gene = ( 'HES4' => 57801, 'TMEM52' => '339456', 'SSU72' => '29101', 'GNB1' => '2782', 'PLEKHN1' => 84069, 'NCRNA00115' => 79854, 'ATAD3A' => '55210', 'N0C2l' => 26155, 'SLC35E2' => '9906', 'ATAD3B' => '83858', 'SAMD11' => 148398 ); $

I hope this is of interest.

Cheers,

JohnGG


Comment on Re: pushing multiple key-value pairs into an existing hash
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-10-26 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls