Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Merging hash

by arivu198314 (Sexton)
on Oct 04, 2011 at 11:01 UTC ( #929506=perlquestion: print w/replies, xml ) Need Help??
arivu198314 has asked for the wisdom of the Perl Monks concerning the following question:

I need to merge two hashes

@val=({23=>1, 43=>1, 34=>1, 66=>1, 99=>1},{33=>1, 64=>1, 35=>1, 54=>1, + 78=>1});

here we have 1 array within two hashes, now i want merge the two hashes and store as 2nd value of array

means

@val=({23=>1, 43=>1, 34=>1, 66=>1, 99=>1},{33=>1, 64=>1, 35=>1, 54=>1, 78=>1}, {23=>1, 43=>1, 34=>1, 66=>1, 99=>, 33=>1, 64=>1, 35=>1, 54=>1, 78=>1});

I have tried below code

$val[2]={$val[0], $val[1]};

It shows compilar error, so please guide me on this

Replies are listed 'Best First'.
Re: Merging hash
by BrowserUk (Pope) on Oct 04, 2011 at 11:08 UTC

    Be aware that if the two smaller hashes contain the same key, then the value that ends up in the composite hash will depend upon the order you list the smaller hashes in the composing statement:

    @val=({23=>1, 43=>1, 34=>1, 66=>1, 99=>1},{33=>1, 64=>1, 35=>1, 54=>1, + 78=>1});; pp \@val;; [ { 23 => 1, 34 => 1, 43 => 1, 66 => 1, 99 => 1 }, { 33 => 1, 35 => 1, 54 => 1, 64 => 1, 78 => 1 }, ] $val[2] = { %{ $val[0] }, %{ $val[ 1 ] } };; pp \@val;; [ { 23 => 1, 34 => 1, 43 => 1, 66 => 1, 99 => 1 }, { 33 => 1, 35 => 1, 54 => 1, 64 => 1, 78 => 1 }, { 23 => 1, 33 => 1, 34 => 1, 35 => 1, 43 => 1, 54 => 1, 64 => 1, 66 +=> 1, 78 => 1, 99 => 1 }, ]

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      It's working :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-10-21 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (119 votes). Check out past polls.

    Notices?