Solved

merging a hash with array of hashes

Posted on 2009-05-06
2
161 Views
Last Modified: 2012-05-06
Can somebody help me append the keys in a hash that are not in an array of hashes? just the keys that are not in that AoH, the rest should remain as it was originally in the AoH. Thanks!

my %Line = ('LIST'=>'2','PAGES'=>'77','EDITION'=>'new');
my @AoH = ({'LIST'=>'2','PAGES'=>'100'},
                      {'LIST'=>'10','EDITION'=>'new'},
                      {'PAGES'=>'30','EDITION'=>'old'},
                      {'LIST'=>'2','PAGES'=>'300'});

desired outcome:

my %Line = ('LIST'=>'2','PAGES'=>'77','EDITION'=>'new');
 my @AoH = ({'LIST'=>'2','PAGES'=>'100','EDITION'=>'new'},
{'LIST'=>'10','PAGES'=>'77','EDITION'=>'new'},
{'LIST'=>'2','PAGES'=>'30','EDITION'=>'old'},
{'LIST'=>'2','PAGES'=>'300','EDITION'=>'old','EDITION'=>'new'});
0
Comment
Question by:cucugirl
  • 2
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 24318412
%$_=(%Line,%$_) for  @AoH;
0
 
LVL 84

Expert Comment

by:ozo
ID: 24318462
#if there are many keys in @AoH, and all the values are true, it may be more efficient to do
for my $h ( @Aoh ){ $h->{$_} ||= $Line{$_} for keys %Line }
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question