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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Merging Data
by Anonymous Monk on Jun 11, 2015 at 00:27 UTC
    I'm not the same person, just having similar issue. Yes I do understand what I do, just asking for some help on how I could merge these two arrays of hashes because the code returns the data that way, your solution with SIMPLE HASHES looks good, I wish I could used it with the original data (ARRAY OF HASHES), thank you!

      Hello again, Anonymous

      The reason that I said I think you are the same person... is because your question and Combining two references happens to have extremely similar data and extremely similar expected output, how about that, what are the odds of that? :D

      As I said Anonymous this forum is all about sharing knowledge and not about feeling offensive or been misunderstood. The reason that I am saying duplicated questions lead to waste of db resources and also waste of time for other Monks seeking knowledge.

      Alternatively you can register your self on the website and then you can have an identity :D instead of Anonymous. You can share your knowledge also in areas that you are more experienced than us :D. Nobody knows everything.

      Any way enough said let's code!!!!! :D

      You can use trippledubs solution: $data1->[0]{$_} = $data2->[0]{$_} for (keys %{$data2->[0]}); if you have one array with hashes on your data same as the data that you provide on your example see $data1 or $data2.

      In case you have more than one arrayHash on your data you can actually use my proposed solution that I found from How do I combine arrays and array of hashes?, that I slightly modified it to meet your criteria ref to arrayHashes.

      Sample of proposed solution:

      #!/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' } ]; my $data3 = [ { 'ACCOUNT5' => '1c', 'ACCOUNT6' => '2c', 'ACCOUNT7' => '3c', }, { 'ACCOUNT8' => '4c', 'ACCOUNT9' => '5c', 'ACCOUNT10' => '6c' } ]; $data1 = [ ({map { %$_ } (@$data1, @$data2, @$data3)}) ]; print Dumper $data1; __DATA__ $VAR1 = [ { 'ACCOUNT5' => '1c', 'ACCOUNT3' => '3b', 'ACCOUNT2' => '2b', 'NUMBER' => '00001', 'ACCOUNT7' => '3c', 'ACCOUNT8' => '4c', 'NAME' => 'JOE DOE', 'ACCOUNT1' => '1b', 'ACCOUNT6' => '2c', 'ACCOUNT4' => '3d', 'ACCOUNT10' => '6c', 'ACCOUNT9' => '5c', 'DATE' => '2015-05-05' } ];

      But be very Careful with the keys of hashes! I mean on your example $data1 you have:

      my $data1 = [ { 'NAME' => 'JOE DOE', 'DATE' => '2015-05-05', 'NUMBER' => '00001' } ];

      If you are planning to use multiple arrayHashes into one and it happens to have the same keys then they will be overwritten!!!!!!

      Sample explanation 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' } ]; my $data3 = [ { 'ACCOUNT5' => '1c', 'ACCOUNT6' => '2c', 'ACCOUNT7' => '3c', }, { 'ACCOUNT8' => '4c', 'ACCOUNT9' => '5c', 'ACCOUNT10' => '6c' } ]; my $data4 = [ { 'NAME' => 'Anonymous Monk', 'DATE' => '2015-06-11', 'NUMBER' => '00005' } ]; $data1 = [ ({map { %$_ } (@$data1, @$data2, @$data3, $data4)}) ]; print Dumper $data1; __DATA__ $VAR1 = [ { 'ACCOUNT5' => '1c', 'ACCOUNT3' => '3b', 'ACCOUNT2' => '2b', 'NUMBER' => '00001', 'ACCOUNT7' => '3c', 'ACCOUNT8' => '4c', 'NAME' => 'JOE DOE', 'ACCOUNT1' => '1b', 'ACCOUNT6' => '2c', 'ACCOUNT4' => '3d', 'ACCOUNT10' => '6c', 'ACCOUNT9' => '5c', 'DATE' => '2015-05-05' } ]; $VAR1 = [ { 'ACCOUNT7' => '3c', 'ACCOUNT3' => '3b', 'ACCOUNT8' => '4c', 'ACCOUNT2' => '2b', 'ACCOUNT5' => '1c', 'ACCOUNT4' => '3d', 'NAME' => 'Anonymous Monk', 'ACCOUNT6' => '2c', 'ACCOUNT10' => '6c', 'NUMBER' => '00005', 'DATE' => '2015-06-11', 'ACCOUNT9' => '5c', 'ACCOUNT1' => '1b' } ];

      Observe the $data4 is similar to $data1, it contains SAME KEYS and DIFFERENT VALUES. The result is obvious, Perl can not understand that you have duplicated keys it overwrites them!!!!! This is why we use ARRAYS OF HASHES.

      Sample of code based on your data input:

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data1 = [ { 'NAME' => 'JOE DOE', 'DATE' => '2015-05-05', 'NUMBER' => '00001' }, { 'NAME' => 'Anonymous Monk', 'DATE' => '2015-06-11', 'NUMBER' => '00005' } ]; print Dumper $data1; __DATA__ $VAR1 = [ { 'DATE' => '2015-05-05', 'NUMBER' => '00001', 'NAME' => 'JOE DOE' }, { 'NAME' => 'Anonymous Monk', 'NUMBER' => '00005', 'DATE' => '2015-06-11' } ];

      Hope this clears more your question and hope based on my solution and explanation I have answered all of your queries.

      Seeking for Perl wisdom...on the process of learning...not there...yet!