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

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

by choroba (Abbot)
on Feb 11, 2013 at 17:02 UTC ( #1018186=note: print w/ replies, xml ) Need Help??


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

If you want to add new key-value pairs one by one, you can use the scalar form:

$gene{$key} = $value;
The slice form can be used if you are inserting a hash into a hash, which is not the case here.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: pushing multiple key-value pairs into an existing hash
Download Code
Re^2: pushing multiple key-value pairs into an existing hash
by lomSpace (Scribe) on Feb 11, 2013 at 17:41 UTC

    I don't want to add them one by one nor create another hash and combine the two. I was wondering if there is a way to take what's under __DATA__ and add each key-value pair dynamically?

      I do not undestand. __DATA__ is a filehandle. There is no method to add a filehandle's content to a hash. In your example, though, you are using
      while (<DATA>) {
      which seems like iterating over the filehandle line by line. What exactly do you need? Why is adding the pairs one by one or creating a temporary hash not an option?
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        If adding a temp hash is the route then I can do that.

        I went ahead and used the map function. Did you have something else in mind?

      I was wondering if there is a way to take what's under __DATA__ and add each key-value pair dynamically?

      How would that be done statically?

      I don't want to add them one by one

      Why not?

      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?

        Yes!,/p>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-07-29 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls