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.
In reply to Re^3: Merging Data
by thanos1983
in thread Merging Data
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |