Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Append to hash reference

by Anonymous Monk
on Nov 18, 2012 at 16:15 UTC ( #1004433=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I have hash reference that holds some values. I would like to add new values to it from another hash reference. What's the best way to do it?

I'm using Perl 5 version 12.

Comment on Append to hash reference
Re: Append to hash reference
by tobyink (Abbot) on Nov 18, 2012 at 16:20 UTC
    use Data::Dumper; my $ref1 = { foo => 1, bar => 2 }; my $ref2 = { baz => 3, quux => 4 }; # Assign to the ref1 hash the data in both hashes %$ref1 = (%$ref1, %$ref2); print Dumper $ref1;

    Or...

    use Data::Dumper; my $ref1 = { foo => 1, bar => 2 }; my $ref2 = { baz => 3, quux => 4 }; # Loop through the keys of the second hash, assigning values into the +first $ref1->{$_} = $ref2->{$_} for keys %$ref2; print Dumper $ref1;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Or...

      use Data::Dumper; my $ref1 = { foo => 1, bar => 2 }; my $ref2 = { baz => 3, quux => 4 }; # Use a hash slice to override values from the first hash with the sec +ond hash @$ref1{ keys %$ref2 } = values %$ref2;
Re: Append to hash reference
by Anonymous Monk on Nov 19, 2012 at 11:09 UTC

    Thanks everyone :)<?p>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-10-22 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls