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!

In reply to Re: Merging Data by thanos1983
in thread Merging Data by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.