http://www.perlmonks.org?node_id=1018211


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?