Re: join two hashes
by moritz (Cardinal) on Jul 30, 2011 at 10:18 UTC
|
You iterate over the keys, and then for each key add a hashref of both values to the new hash.
See perlreftut for the perly details if you have troubles putting it into code.
| [reply] |
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;
or
my %new_hash = map {($_, [$rep{$_}, $comb{$_}])} keys %rep;
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
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.
| [reply] |
|
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.
| [reply] [d/l] |
|
|
|
|
What does that mean? Its like saying the answer to Life, the Universe, and Everything, is 42 -- does not compute
| [reply] |
Re: join two hashes
by planetscape (Chancellor) on Jul 30, 2011 at 12:26 UTC
|
| [reply] |
Re: join two hashes
by persianswallow (Novice) on Jul 30, 2011 at 11:38 UTC
|
my @key=keys %rep;
for my $key(@key){
$new_hash{$key}="[$rep{$key},$comb{$key}]";}
please professionals tell me is it correct or not. | [reply] [d/l] |
|
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 :)
| [reply] |
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. | [reply] |
|
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
| [reply] |
|
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.
| [reply] [d/l] |
|
|