[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

hey guys

i want to loop over hash1 and check the keys of this hash and compare them to the keys of hash 2.. if hash1 does not have a key from hash 2 i need to copy this key to hash1 with its value.
0
cucugirl
Asked:
cucugirl
  • 2
1 Solution
 
Adam314Commented:

foreach (keys %hash2) {
    next if exists($hash1{$_});
    $hash1{$_}=$hash2{$_};
}

Open in new window

0
 
ozoCommented:
%hash1 = (%hash2,%hash1);
0
 
ozoCommented:
 %hash1 = (%hash2,%hash1)
performs the same function, albeit without an explicit loop.
if you prefer an explicit loop, and you have perl5.10 or later, you might use
  $hash1{$_} //= $hash2{$_} for keys %hash2;  #assuming existing values are defined
prior to 5.10, if the values in %hash1 are all true, you could do
   $hash1{$_} ||= $hash2{$_} for keys %hash2;
otherwise you could
  exists $hash1{$_} or $hash1{$_} = $hash2{$_} for keys %hash2;
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now