Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Merge hashrefs into one

by Anonymous Monk
on Jul 14, 2020 at 16:13 UTC ( #11119307=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I need to merge this data into one dataset, I tried this:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # This is the dump of a sample data and added to $var1 $var2 $var3 my $var1 = [ { 'date' => '2001-06-04', 'number' => '12345', 'amount' => '100.00', 'status' => 'paid', 'type' => 'new' }, { 'date' => '2000-001-02', 'number' => 'xc234', 'amount' => '30.88', 'status' => 'new', 'type' => 'cost' } ]; my $var2 = [ { 'ppay' => 'Smith Doe' } ]; my $var3 = [ { 'deb1' => '0', 'cred' => '0', 'addr' => '100 - Main Street', 'total' => '250.00 usd', }, { 'deb1' => '0', 'cred' => '50.14', 'addr' => '1 - Central', 'total' => '51.00', } ]; my ($data) = {$var1, $var2, $var3}; print Dumper $data;

Result:

Odd number of elements in anonymous hash at merge.pl line 53.
$VAR1 = { 'ARRAY(0x14bd858)' => [ { 'ppay' => 'Smith Doe' } ], 'ARRAY(0x14cb430)' => undef };


I am looking for this data dump:

$VAR! = [ { 'date' => '2001-06-04', 'number' => '12345', 'amount' => '100.00', 'status' => 'paid', 'type' => 'new' }, { 'date' => '2000-001-02', 'number' => 'xc234', 'amount' => '30.88', 'status' => 'new', 'type' => 'cost' }, { 'ppay' => 'Smith Doe' }, { 'deb1' => '0', 'cred' => '0', 'addr' => '100 - Main Street', 'total' => '250.00 usd', }, { 'deb1' => '0', 'cred' => '50.14', 'addr' => '1 - Central', 'total' => '51.00', } ];

Thanks for the help!

Replies are listed 'Best First'.
Re: Merge hashrefs into one
by haukex (Bishop) on Jul 14, 2020 at 16:24 UTC

    $var1, $var2, and $var3 are each array references that you need to dereference to get their elements. {$var1, $var2, $var3} constructs an anonymous hash, not an array, you want [] instead. my $data = [@$var1, @$var2, @$var3]; produces your expected output. References: perldsc, perlreftut, perlref.

Re: Merge hashrefs into one
by LanX (Cardinal) on Jul 14, 2020 at 16:19 UTC
    var1-3 are array (AoH) references, hence scalar and not hashes.

    This my ($data) = {$var1, $var2, $var3}; to create a hash doesn't make any sense, the error is correct.

    I don't understand what you're desired output is, so please specify.

    Try $a_new= [ @$var1, @$var2, @$var3 ]

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Merge hashrefs into one
by Marshall (Canon) on Jul 16, 2020 at 00:05 UTC
    The first task here is to work backwards from your Dumper output to something like you have in the actual source code.
    In the future, please show us the source code.

    I created 3 arrays of references to hash.
    When Dumper, dumps those arrays it uses $VARx=reference to array instead of actual names in your source code.
    The below shows one possibility.
    "Merge" is not right description here, "combine" seems more descriptive.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @x = ( { 'date' => '2001-06-04', 'number' => '12345', 'amount' => '100.00', 'status' => 'paid', 'type' => 'new' }, { 'date' => '2000-001-02', 'number' => 'xc234', 'amount' => '30.88', 'status' => 'new', 'type' => 'cost' } ); my @y = ( { 'ppay' => 'Smith Doe' } ); my @z = ( { 'deb1' => '0', 'cred' => '0', 'addr' => '100 - Main Street', 'total' => '250.00 usd', }, { 'deb1' => '0', 'cred' => '50.14', 'addr' => '1 - Central', 'total' => '51.00', } ); print "This replicates your dumper output:\n"; print Dumper \@x,\@y,\@z; #This is what you need in your source code: my @combined = (@x,@y,@z); print "\n####### Desired Dumper Output ####\n"; print Dumper \@combined; __END__ This replicates your dumper output: $VAR1 = [ { 'type' => 'new', 'status' => 'paid', 'amount' => '100.00', 'number' => '12345', 'date' => '2001-06-04' }, { 'number' => 'xc234', 'date' => '2000-001-02', 'amount' => '30.88', 'status' => 'new', 'type' => 'cost' } ]; $VAR2 = [ { 'ppay' => 'Smith Doe' } ]; $VAR3 = [ { 'addr' => '100 - Main Street', 'total' => '250.00 usd', 'cred' => '0', 'deb1' => '0' }, { 'addr' => '1 - Central', 'total' => '51.00', 'cred' => '50.14', 'deb1' => '0' } ]; ####### Desired Dumper Output #### $VAR1 = [ { 'type' => 'new', 'status' => 'paid', 'amount' => '100.00', 'number' => '12345', 'date' => '2001-06-04' }, { 'number' => 'xc234', 'date' => '2000-001-02', 'amount' => '30.88', 'status' => 'new', 'type' => 'cost' }, { 'ppay' => 'Smith Doe' }, { 'addr' => '100 - Main Street', 'total' => '250.00 usd', 'cred' => '0', 'deb1' => '0' }, { 'addr' => '1 - Central', 'total' => '51.00', 'cred' => '50.14', 'deb1' => '0' } ];
    Update: As a side note: The order that hash keys appear are random. And in more recent Perl's that is guaranteed to be so to prevent certain types of security problems. There is a "fudge factor" that gets added into the hashing function that changes per run of even the same Perl program.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11119307]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2020-09-18 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (112 votes). Check out past polls.

    Notices?