in reply to multiple hash compare, find, create

Hello Anonymous Monk,

I see the monks have replied to your question. I saw the question 2 - 3 hours ago and I start working on it but I got busy with something else work related.

Any way just for fun (it took so much time to prepare the hashes :P).

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash_1 = ( "a6fbb013-b75f-4dd7-9d1a-24f566020042" => "92.1.3", "b6a4c433-72a5-4e1a-b378-4a6b72531ded" => "92.1.3", "P0118760075" => "92.1.3", "9d986ace-2504-4595-bdbb-1899812e9d54" => "91.1.2", "55ccc30e-3566-4a00-b219-4b084487384c" => "92.1.3", "P0127439637" => "92.1.3", "08141110-f817-4c16-bf7b-8d0e6696a95b" => "91.1.2", "71be5e75-edad-4889-9261-e1ffa89e393f" => "92.1.3", "c6e0b7c8-4999-4e83-b7d9-c28a62613614" => "92.1.3", "e173efe4-76f8-47fa-9923-500a3fe9715d" => "92.1.0", "P0130482072" => "92.1.3", "a915d30a-541c-4f5e-9b2f-297352f7e19c" => "92.1.3", "64bc2611-38a6-4a59-8d80-4f49b7a76f69" => "92.1.3", "205dae51-ea2e-4db9-ace1-315b940686e6" => "91.1.2" ); my %hash_2 = ( "5f356d12-0213-4d5f-8fe7-08fe1d2a35d9" => "3.2p10s2", "dff7f963-ec15-440a-9150-b61f55afe8a4" => "3.2p9s1", "P0107577526" => "3.2p3s1", "P0112055731" => "3.2p10s1", "06bbe788-e57d-4eda-98ea-74d8a45a0e56" => "3.2p10s1", "P0127646016" => "3.2p10s1", "b4206f77-25e9-4ccd-b434-2237360f1f8c" => "3.1p10s1", "97891097-70d7-4273-b1ae-3b88b460d591" => "3.2p8s3", "c8b2958f-7777-45e2-929a-adbe41f5055f" => "3.2p10s1", "6ad8af7c-c56b-480f-bed6-9591b80cf634" => "3.0p9s1", "2e530dc0-a164-4c06-ae18-332eb6778ebd" => "3.2p10s1", "P0116761501" => "3.2p10s1", "37d6871a-3abc-44ee-819a-eea33440b0a4" => "3.1p7s7", "P0128132579" => "3.2p10s1" ); my %hash_3 = ( "P0128193326" => "8993110670064343", "a6fbb013-b75f-4dd7-9d1a-24f566020042" => "8499101090018240 +", "29568879-fcca-4dc6-86be-3c8c86ef26db" => "8497101420498122 +", "9d986ace-2504-4595-bdbb-1899812e9d54" => "8773103910046051 +", "P0118760075" => "8495840020455261", "55ccc30e-3566-4a00-b219-4b084487384c" => "8498350112590600 +", "P0127439637" => "8155600386311784", "71be5e75-edad-4889-9261-e1ffa89e393f" => "8773103940365745 +", "c6e0b7c8-4999-4e83-b7d9-c28a62613614" => "8495741441414558 +", "e173efe4-76f8-47fa-9923-500a3fe9715d" => "8773102120218526 +", "P0130482072" => "8499100024861022", "a915d30a-541c-4f5e-9b2f-297352f7e19c" => "8777703187635225 +", "205dae51-ea2e-4db9-ace1-315b940686e6" => "8299600120059407 +", "b2c6e317-2072-4e3a-9278-5f76af49221a" => "8499102590027251 +" ); my @AoH; push @AoH, \%hash_1, \%hash_2, \%hash_3; # print Dumper \@AoH; my %HoA; foreach my $hash (@AoH) { foreach my $key (keys %$hash) { push(@{$HoA{$key}}, %$hash{$key}) if exists $hash_1{$key}; } } print Dumper \%HoA; __END__ $ perl test.pl $VAR1 = { 'a6fbb013-b75f-4dd7-9d1a-24f566020042' => [ 'a6fbb013-b75f-4 +dd7-9d1a-24f566020042', '92.1.3', 'a6fbb013-b75f-4 +dd7-9d1a-24f566020042', '849910109001824 +0' ], 'a915d30a-541c-4f5e-9b2f-297352f7e19c' => [ 'a915d30a-541c-4 +f5e-9b2f-297352f7e19c', '92.1.3', 'a915d30a-541c-4 +f5e-9b2f-297352f7e19c', '877770318763522 +5' ], '205dae51-ea2e-4db9-ace1-315b940686e6' => [ '205dae51-ea2e-4 +db9-ace1-315b940686e6', '91.1.2', '205dae51-ea2e-4 +db9-ace1-315b940686e6', '829960012005940 +7' ], 'P0118760075' => [ 'P0118760075', '92.1.3', 'P0118760075', '8495840020455261' ], '55ccc30e-3566-4a00-b219-4b084487384c' => [ '55ccc30e-3566-4 +a00-b219-4b084487384c', '92.1.3', '55ccc30e-3566-4 +a00-b219-4b084487384c', '849835011259060 +0' ], '08141110-f817-4c16-bf7b-8d0e6696a95b' => [ '08141110-f817-4 +c16-bf7b-8d0e6696a95b', '91.1.2' ], '9d986ace-2504-4595-bdbb-1899812e9d54' => [ '9d986ace-2504-4 +595-bdbb-1899812e9d54', '91.1.2', '9d986ace-2504-4 +595-bdbb-1899812e9d54', '877310391004605 +1' ], 'c6e0b7c8-4999-4e83-b7d9-c28a62613614' => [ 'c6e0b7c8-4999-4 +e83-b7d9-c28a62613614', '92.1.3', 'c6e0b7c8-4999-4 +e83-b7d9-c28a62613614', '849574144141455 +8' ], 'b6a4c433-72a5-4e1a-b378-4a6b72531ded' => [ 'b6a4c433-72a5-4 +e1a-b378-4a6b72531ded', '92.1.3' ], '71be5e75-edad-4889-9261-e1ffa89e393f' => [ '71be5e75-edad-4 +889-9261-e1ffa89e393f', '92.1.3', '71be5e75-edad-4 +889-9261-e1ffa89e393f', '877310394036574 +5' ], 'P0127439637' => [ 'P0127439637', '92.1.3', 'P0127439637', '8155600386311784' ], 'e173efe4-76f8-47fa-9923-500a3fe9715d' => [ 'e173efe4-76f8-4 +7fa-9923-500a3fe9715d', '92.1.0', 'e173efe4-76f8-4 +7fa-9923-500a3fe9715d', '877310212021852 +6' ], '64bc2611-38a6-4a59-8d80-4f49b7a76f69' => [ '64bc2611-38a6-4 +a59-8d80-4f49b7a76f69', '92.1.3' ], 'P0130482072' => [ 'P0130482072', '92.1.3', 'P0130482072', '8499100024861022' ] };

I would follow the same approach as fellow Monk stevieb. First push all arrays to an ARRAYS OF HASHES and then iterate over them compare with hash 1 if the key is in hash_1 then keep it and create an HASHES OF ARRAYS.

Hope this helps, BR.

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

Replies are listed 'Best First'.
Re^2: multiple hash compare, find, create
by AnomalousMonk (Archbishop) on Dec 10, 2018 at 23:20 UTC

    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:  <%-{-{-{-<

      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!