in reply to Merging Data
Hello Anonymous,
When you are saying I looked at a similar post here please provide with a link not only to assist more but also for future references. I assume that you referring to Combining two references.
Can you please manually produce the output because is confusing what you are describing. On the link that I provided you will found several answers similar to your question.
Update: Have you seen Hash::Merge.
Update 2: By reading your question again and again, since I think you are the same person who post this question Combining two references yesterday and from my point of view it looks that you did not get the answer that you where looking, so you created another question.
Any way, since this site is about helping people, but in future I would recommend you to stay with one question instead of duplicating. People still follow the posts for days, to see if they have been solved.
Having said that I am not so sure if you have understand the difference between simple hash (perldata), hash ref (perlref) and ARRAYS OF HASHES.
The way that you define $data1 and $data2 it is like having an array of hashes and then you made a reference to it.
Sample of code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data1 = ( { 'NAME' => 'JOE DOE', 'DATE' => '2015-05-05', 'NUMBER' => '00001' } ); print Dumper \@data1; my $arrayrefData1 = \@data1; print Dumper $arrayrefData1; __DATA__ $VAR1 = [ { 'NAME' => 'JOE DOE', 'NUMBER' => '00001', 'DATE' => '2015-05-05' } ]; $VAR1 = [ { 'NAME' => 'JOE DOE', 'NUMBER' => '00001', 'DATE' => '2015-05-05' } ];
As you can see on the example above you have an array of hashes and you have assign it to a reference. So based on this I believe that you found this sample of code on line you modified it based on what you want to do, but you do not understand how to use it. I recommend go and READ all the links that I provided you, you will learn so much through them and then you can understand what you do.
Since I think you do not understand what you do because you have created two array of hashes with only one array hash element on each. I am referring to $data1 and $data2, this does not make any sense. I am proposing a much more simpler solution to your problem.
Two SIMPLE hashes %data1 and %data2 combined in two different ways. Number or keys on each hash it is not important, since you can merge them even if they do not have equal keys.
Sample of code:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %data1 = ( 'NAME' => 'JOE DOE', 'DATE' => '2015-05-05', 'NUMBER' => '00001' ); my %data2 = ( 'ACCOUNT1' => '1b', 'ACCOUNT2' => '2b', 'ACCOUNT3' => '3b', 'ACCOUNT4' => '3d' ); =solution1 my %newHash = (%data1, %data2); print Dumper \%newHash; =cut =solution2 @data1{keys %data2} = values %data2; print Dumper \%data1; =cut __DATA__ $VAR1 = { 'ACCOUNT4' => '3d', 'ACCOUNT3' => '3b', 'ACCOUNT2' => '2b', 'ACCOUNT1' => '1b', 'NUMBER' => '00001', 'DATE' => '2015-05-05', 'NAME' => 'JOE DOE' };
Read also how to merge Hash, it contains some basic information and also different ways of accomplishing hash merging.
Update 3: Please change the title of your question from "Merging Data" to something like "Merging Hashes", it describes what you are trying to achieve.
Hope this helps more than just a simple answer to your problem! :D.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Merging Data
by Anonymous Monk on Jun 11, 2015 at 00:27 UTC | |
by thanos1983 (Parson) on Jun 11, 2015 at 09:05 UTC |