http://qs1969.pair.com?node_id=917651


in reply to Re: join two hashes
in thread join two hashes

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

Replies are listed 'Best First'.
Re^3: join two hashes
by johngg (Canon) on Jul 31, 2011 at 02:05 UTC

    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