Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

join two hashes

by Anonymous Monk
on Jul 30, 2011 at 10:15 UTC ( #917600=perlquestion: print w/replies, xml ) Need Help??

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

hi friends,who knows, what is the best way to combine two hashes? these hashes have a same keys but different i want to assign two value to one key.

$comb{$element}=$comb_number; $rep{$element}=$rep_number;

i want a new hash as follow:

 $new_hash{$element}=[$rep_number  $comb_number]

Replies are listed 'Best First'.
Re: join two hashes
by moritz (Cardinal) on Jul 30, 2011 at 10:18 UTC
Re: join two hashes
by JavaFan (Canon) on Jul 30, 2011 at 10:19 UTC
    "Best way"? By which measurement? I may write it as (untested code):
    my %new_hash; $new_hash{$_} = [$rep{$_}, $comb{$_}] for keys %rep;
    my %new_hash = map {($_, [$rep{$_}, $comb{$_}])} keys %rep;

      in this case i have this error

        It is not an error. The value of the key is the array reference as you wanted (or at least we can guess so, because a comma is missing in your spec). See perlref.
        You mean, if you run my code, the error you get is key=ARRAY<0x1889c64>? That would be quite strange, as that's not a standard Perl error. And the code snippet I gave doesn't do any I/O by itself.
        What does that mean? Its like saying the answer to Life, the Universe, and Everything, is 42 -- does not compute
Re: join two hashes
by planetscape (Chancellor) on Jul 30, 2011 at 12:26 UTC
Re: join two hashes
by persianswallow (Novice) on Jul 30, 2011 at 11:38 UTC

    i am beginner but i did it as this code

    my @key=keys %rep; for my $key(@key){ $new_hash{$key}="[$rep{$key},$comb{$key}]";}

    please professionals tell me is it correct or not.

      Well, it doesn't accomplish your stated goal, storing two values for one key, which is accomplished by storing an array reference

      What this does is store one value, a scalar, a string

      If this is what you want, then it is correct :)

Re: join two hashes
by Anonymous Monk on Jul 30, 2011 at 13:14 UTC
    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.

      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.



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2022-09-29 07:17 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (125 votes). Check out past polls.