Solved

merging a hash with array of hashes

Posted on 2009-05-06
2
155 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now