Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Kenosis (Priest)
on Feb 11, 2013 at 18:23 UTC ( #1018211=note: print w/ replies, xml ) Need Help??


in reply to Re^2: pushing multiple key-value pairs into an existing hash
in thread pushing multiple key-value pairs into an existing hash

You said, ...push multiple key-value pairs onto an existing hash. Just to clarify, values (scalars) are pushed onto an array; you don't push onto a hash.

The push in your while loop adds array references as values associated with keys, so you see a mix of data from the Dumper output (your push @{$gene{$key}}, $value; notation says treat the key's associated value as an array and push the contents of $value onto that array):

$VAR1 = { 'HES4' => '57801', 'TMEM52' => [ '339456' ], 'SSU72' => [ '29101' ], 'GNB1' => [ '2782' ], 'PLEKHN1' => '84069', 'NCRNA00115' => '79854', 'ATAD3A' => [ '55210' ], 'ATAD3B' => [ '83858' ], 'SLC35E2' => [ '9906' ], 'SAMD11' => '148398', 'NOC2L' => '26155' };

The [ ] notation indicates a list--each one having been generated by the push--so your hash now contains array references and non-reference values.

choroba has shown you how to add the key-value pairs to an existing hash (which will overwrite an existing value, if the key already exsits):

while(<DATA>){ chomp; my ($key, $value) = split; $gene{$key} = $value; # choroba's suggestion }

Dumper output:

$VAR1 = { 'HES4' => '57801', 'TMEM52' => '339456', 'SSU72' => '29101', 'GNB1' => '2782', 'PLEKHN1' => '84069', 'NCRNA00115' => '79854', 'ATAD3A' => '55210', 'ATAD3B' => '83858', 'SLC35E2' => '9906', 'SAMD11' => '148398', 'NOC2L' => '26155' };

Is this what you wanted to achieve?


Comment on Re^3: pushing multiple key-value pairs into an existing hash
Select or Download Code
Re^4: pushing multiple key-value pairs into an existing hash
by lomSpace (Scribe) on Feb 11, 2013 at 18:36 UTC

    Yes!,/p>

      You were just that close...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-25 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls