Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by lomSpace (Scribe)
on Feb 11, 2013 at 17:41 UTC ( #1018197=note: print w/ replies, xml ) Need Help??


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

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?


Comment on Re^2: pushing multiple key-value pairs into an existing hash
Re^3: pushing multiple key-value pairs into an existing hash
by choroba (Abbot) on Feb 11, 2013 at 17:45 UTC
    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?

        Did map produce the results you want? If so, post your code, then someone can tell you if there is a better way.

        By the way, map adds the key/value pairs one by one--just like a while loop.

Re^3: pushing multiple key-value pairs into an existing hash
by 7stud (Deacon) on Feb 11, 2013 at 18:21 UTC

    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?
Re^3: pushing multiple key-value pairs into an existing hash
by Kenosis (Priest) on Feb 11, 2013 at 18:23 UTC

    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>

        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://1018197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-09-01 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls