in reply to Re: multiple hash compare, find, create
in thread multiple hash compare, find, create

Why is the key "a6fbb013-b75f-4dd7-9d1a-24f566020042" present in the output hash when it seems to appear only in the %hash_1 and %hash_3 input hashes and not in the %hash_2 input hash? Why is this key present in the value-set array for this key in the output hash? (Indeed, every key in the output hash seems to be present in each associated value-set array.) This does not seem to comport with the output that supertaco wants.

I would follow the same approach as fellow Monk stevieb.

But stevieb has endorsed Eily's approach.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: multiple hash compare, find, create
by thanos1983 (Parson) on Dec 11, 2018 at 11:31 UTC

    Hello AnomalousMonk,

    You are right. My solution after a bit of experimentation I can see that it is wrong. The best approach that I could think is similar to fellow Monk Eily.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %h1 = ( a => 1, b => 2, c => 3, ); my %h2 = ( a => 4, b => 5, d => 10, ); my %h3 = ( x => 20, p => 15, b => 6, a => 12, ); my %HoA; foreach my $key (keys %h1) { push(@{$HoA{$key}}, $h1{$key}, $h2{$key}, $h3{$key}) if exists $h2{$key} and exists $h3{$key}; } print Dumper \%HoA; __END__ $ perl test.pl $VAR1 = { 'b' => [ 2, 5, 6 ], 'a' => [ 1, 4, 12 ] };

    Though Eily's approach is better using unless as it check if the hash exists if not skip not necessary to proceed and waste resources.

    Thanks for spending the time to check and correct me :)

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