Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: join two hashes

by Anonymous Monk
on Jul 30, 2011 at 13:14 UTC ( [id://917622]=note: print w/replies, xml ) Need Help??


in reply to join two hashes

The value for any hash-entry can be any one thing ... but, only one thing. That "one thing" can be a REFERENCE to anything; a hash, an array. This solves your requirement but changes your code.

Replies are listed 'Best First'.
Re^2: join two hashes
by koolgirl (Hermit) on Jul 30, 2011 at 21:50 UTC

    Thanks, that cleared up a few things for me..I was quite perplexed at the thought of creating key-value pairs, with multiple values, (of course, once there's more than two, it would no longer be a technical "pair"). I mean not only does the entire concept make my brain hurt, but I thought the idea of multiple values per key sort of defeated the purpose of creating a hash altogether (not to mention the idea of how to get to each individual value and the complications arising from that).

    Thinking of it along the lines as each key still only having "one" value, that "one" value being a reference to other lists (or, arrays, rather)/hashes, makes a little more sense to me, but, ultimately, still hurts my brain...oO

      One way to ease the brain ache is to use Data::Dumper to visualise your data structures.

      knoppix@Microknoppix:~$ perl -MData::Dumper -Mstrict -wE ' > my %rep = ( one => 123, two => 456 ); > my %comb = ( one => 987, two => 654 ); > my %new = map { $_, [ $rep{ $_ }, $comb{ $_ } ] } keys %rep; > print Data::Dumper->Dumpxs( > [ \ %rep, \ %comb, \ %new ], > [ qw{ *rep *comb *new } ] > ); > say q{-} x 30; > push @{ $new{ two } }, 888; > $new{ three } = { four => 444, five => 555 }; > print Data::Dumper->Dumpxs( [ \ %new ], [ qw{ *new } ] );' %rep = ( 'one' => 123, 'two' => 456 ); %comb = ( 'one' => 987, 'two' => 654 ); %new = ( 'one' => [ 123, 987 ], 'two' => [ 456, 654 ] ); ------------------------------ %new = ( 'three' => { 'five' => 555, 'four' => 444 }, 'one' => [ 123, 987 ], 'two' => [ 456, 654, 888 ] ); knoppix@Microknoppix:~$

      I hope this is helpful.

      Cheers,

      JohnGG

        It does, actually, works just like tylenol! I'm actually trying to learn how to work with modules at the moment, because I really haven't been doing much of that at all and I know I'm limiting my growth by not, so this is a great starting point for me to begin working with one on a specific point of interest. Thanks :D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found