Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creati ng new HashArray from 2 existing Hashes

Status
Not open for further replies.

not4google

Programmer
Nov 6, 2006
15
0
0
GB
Hi all,

I have 2 HashArrays:
%a
%b

%a contains the following key/value pairs:
Forward/Value

%b contains the following key/value pairs:
Curve/Forward

What I need to do is create a new HashArray that is a combination of %a and %b and has the following key/value:
Curve/Value

Any assistance would be much appreciated,
 
What if the value in %b doesn't exist in %a? If this is an issues leaving in the grep statement before. If not, remove it.

Code:
my %c = map {$_ => $a{$b{$_}}} grep {exists $a{$b{$_}} keys %b;

- Miller
 
Hi Thanks for the response,

If you could explain this line in a bit more detail that would be really useful as Im new to Perl,

Thanks,
 
Try it this way ...
Code:
my %c;
for (keys(%b)) {
  $c{$_} = $a{$b{$_}} unless !exists($a{$b{$_}});
}

Hopefully this will make more sense. We want to map the keys of %b to the values of %a, but as Miller pointed out, we need to check that a given key (from the values of %b) exists in %a. Otherwise you'll get an uninitialized value warning.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top