Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018271]
help
Chatterbox?
[Eily]: Discipulus that's a chess joke BTW right? Because bishops don't walk straight :D
[choroba]: stop making chessy jokes about bishops!
[choroba]: I mean cheesy
[LanX]: lol
[ambrus]: no, the chess bishop itself is a joke on real bi-shops
[ambrus]: or at least on the stereotype of bi-shops
[choroba]: we call chess bishops "archers"
[Eily]: choroba sorry, I had to get that off my chess
[ambrus]: chess certainly hasn't started those stereotypes, like how kings aren't that powerful but has convinced their whole country to work for them, etc. it's just poured them to a nice clean form that would easily get propagated.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-09-26 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (294 votes). Check out past polls.

    Notices?