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

how to merge Hash

by jesuashok (Curate)
on Jan 20, 2006 at 13:03 UTC ( #524456=perlquestion: print w/replies, xml ) Need Help??

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

How to Merge two hash in

hash1 and its Keys hash2 and its Keys
is there any function are module available to do this Task ?

"Keep pouring your ideas"

Replies are listed 'Best First'.
Re: how to merge Hash
by osunderdog (Deacon) on Jan 20, 2006 at 13:11 UTC

    %newHash = (%hash1, %hash2);

    Hazah! I'm Employed!

Re: how to merge Hash
by blazar (Canon) on Jan 20, 2006 at 13:35 UTC
    my %merge=(%hash1, %hash2); # ? # how 'bout key clashes? my %merge2=(%hash2, %hash1); # will be different => MORE DETAILS - MORE DETAILS!!

    PS: letting apart the fact that the question -if I understood it correctly- is extremely trivial and -still if I understood it correctly- I'd recommend reading some introductory perl document or tutorial before proceeding, I find that your "code" didn't help - to understand correctly your question. If you have some real (pseudo-)code that is suitable to illustrate a problem, then show it, else do not contribute to line noise: help people to help you!

Re: how to merge Hash
by marto (Cardinal) on Jan 20, 2006 at 13:12 UTC
Re: how to merge Hash
by QM (Parson) on Jan 22, 2006 at 16:42 UTC
    Or even
    @hash1{keys %hash2} = values %hash2;
    However, if there are any common keys, the last value assigned will be retained.

    Quantum Mechanics: The dreams stuff is made of

      can you please explain me the above code when you assigned the values of %hash2 to @hash1 how it is reflecting in %hash1

        There is no array "@hash1" there is only the hash "%hash1".

        This is called a hash slice , and in a quirk of perl syntax uses the @ sigil instead of %

        my %foo; @foo{ 'hash', 'slices' } = ( 'use', 'curly braces' );

        my @bar; @bar[ 1,2,3,4 ] = ( 'array', 'slices', 'use', 'square brackets' );

        Say it with me:

        { 'hashes', 'are', 'curly', 'ones' }

        [ 'arrays', 'are', 'square' ]

        ( 'lists are round' )

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2021-01-23 02:14 GMT
Find Nodes?
    Voting Booth?