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

Hi Everyone I was hoping you might be able to help me with the following. I have 3 hashes and I want to iterate through the keys in each one and if I find a key that exists in all 3 hashes I want to then print out the corresponding values onto one line.

This is the content of hash one

bigstor14_mpt nnap5307-s bigstor17_mpt nnap5307-s bigstor12_mpt nnap5307-s bigstorq3_mpt nnap3289-s bigstor13_mpt nnap5307-s bigstor15_mpt nnap5307-s bigstor3_mpt nydevnfs_bigstor_volA bigstor4_mpt nydevnfs_bigstor_volA bigstor19_mpt nnap5307-s bigstor18_mpt nnap5307-s bigstorq4_mpt nnap3290-s bigstorderv_mpt nydevnfs_derv bigstor23_mpt nnap5307-s bigstor1_mpt nydevnfs_bigstor_volA bigstor5_mpt nydevnfs_bigstor_volA bigstor30_mpt nnap5307-s bigstor8_mpt nydevnfs_bigstor_volA bigstorq1_mpt nydevnfs_bigstorq1 bigstor10_mpt nnap5307-s bigstorqird_mpt nnap3290-s bigstorq2_mpt nydevnfs_bigstorq2 bigstor11_mpt nnap5307-s bigstorq0_mpt nydevnfs_bigstorq0 bigstor29_mpt nnap5307-s bigstor7_mpt nydevnfs_bigstor_volA bigstor20_mpt nnap5307-s bigstor16_mpt nnap5307-s bigstor21_mpt nnap5307-s bigstorsas_mpt nydevnfs_sas bigstor2_mpt nydevnfs_bigstor_volA bigstorn_mpt nydevnfs_bigstorn bigstormtg_mpt nydevnfs_mtge

This is the content of hash 2

bigstor14_mpt /vol/bigstor_volB/bigstor14 bigstor17_mpt /vol/bigstor_volC/bigstor17 bigstor12_mpt /vol/bigstor_volB/bigstor12 bigstorq3_mpt /vol/bigstorq3 bigstor13_mpt /vol/bigstor_volB/bigstor13 bigstor15_mpt /vol/bigstor_volB/bigstor15 bigstor3_mpt /vol/bigstor_volA/bigstor3 bigstor4_mpt /vol/bigstor_volA/bigstor4 bigstor19_mpt /vol/bigstor_volC/bigstor19 bigstor18_mpt /vol/bigstor_volC/bigstor18 bigstorq4_mpt /vol/bigstorq4 bigstorderv_mpt /vol/mtge bigstor23_mpt /vol/bigstor_volD/bigstor23 bigstor1_mpt /vol/bigstor_volA/bigstor1 bigstor5_mpt /vol/bigstor_volA/bigstor5 bigstor30_mpt /vol/bigstor_volB/bigstor30 bigstor8_mpt /vol/bigstor_volA/bigstor8 bigstorq1_mpt /vol/bigstorq1 bigstor10_mpt /vol/bigstor_volB/bigstor10 bigstorqird_mpt /vol/bigstor_ird bigstorq2_mpt /vol/bigstorq2 bigstor11_mpt /vol/bigstor_volB/bigstor11 bigstorq0_mpt /vol/bigstorq0 bigstor29_mpt /vol/bigstor_volB/bigstor29 bigstor7_mpt /vol/bigstor_volA/bigstor7 bigstor20_mpt /vol/bigstor_volC/bigstor20 bigstor16_mpt /vol/bigstor_volB/bigstor16 bigstor21_mpt /vol/bigstor_volC/bigstor21 bigstorsas_mpt /vol/sas bigstor2_mpt /vol/bigstor_volA/bigstor2 bigstorn_mpt /vol/bigstorn bigstormtg_mpt /vol/mtge

This is the content of hash 3

bigstor14_mpt /bb/bigstor14 bigstor17_mpt /bb/bigstor17 bigstor12_mpt /bb/bigstor12 bigstorq3_mpt /bb/bigstorq3 bigstor13_mpt /bb/bigstor13 bigstor15_mpt /bb/bigstor15 bigstor3_mpt /bb/bigstor3 bigstor4_mpt /bb/bigstor4 bigstor19_mpt /bb/bigstor19 bigstor18_mpt /bb/bigstor18 bigstorq4_mpt /bb/bigstorq4 bigstorderv_mpt /bb/bigstor/derv bigstor23_mpt /bb/bigstor23 bigstor1_mpt /bb/bigstor1 bigstor5_mpt /bb/bigstor5 bigstor30_mpt /bb/bigstor30 bigstor8_mpt /bb/bigstor8 bigstorq1_mpt /bb/bigstorq1 bigstor10_mpt /bb/bigstor10 bigstorqird_mpt /bb/bigstor_ird bigstorq2_mpt /bb/bigstorq2 bigstor11_mpt /bb/bigstor11 bigstorq0_mpt /bb/bigstorq0 bigstor29_mpt /bb/bigstor29 bigstor7_mpt /bb/bigstor7 bigstor20_mpt /bb/bigstor20 bigstor16_mpt /bb/bigstor16 bigstor21_mpt /bb/bigstor21 bigstorsas_mpt /bb/bigstor/sas bigstor2_mpt /bb/bigstor2 bigstorn_mpt /bb/bigstorn bigstormtg_mpt /bb/bigstor/mtgmodel

So for example I want to iterate through the keys in each hash and if the key bigstor14_mpt exists in all 3 hashes then I want to print out the corresponding values onto one line in an output file like this: nnap5307-s:/vol/bigstor_volB/bigstor14:/bb/bigstor14 Then continue the loop and if find another key that exists in all 3 hashes like for example bigstor17_mpt then grab the corresponding values from this key in each hash and then append to the output file the next line like this: nnap5307-s:/vol/bigstor_volC/bigstor17:/bb/bigstor17

I can sort the keys on one hash and print out the key/value pairs but I am really struggling with how to do the above. Please can you help me?

  • Comment on Comparing keys in 3 different hashes and if they are the same them print the values on one line
  • Select or Download Code

Replies are listed 'Best First'.
Re: Comparing keys in 3 different hashes and if they are the same them print the values on one line
by kcott (Archbishop) on Mar 01, 2013 at 04:24 UTC

    G'day NewLondonPerl1,

    You don't need to iterate through each hash, you only need to iterate through one of them because if the key doesn't exist in one then the condition of the key existing in all three is not met. If they vary substantially in size, pick the one with the least number of keys. The guts of your code will look something like:

    for (keys %hash1) { if (exists $hash2{$_} and exists $hash3{$_}) { print $hash1{$_}, $hash2{$_}, $hash3{$_}; } }

    You don't need to sort for the functionality to work. I'll leave you to format the output.

    -- Ken

      Thanks ever so much Ken. This is xactly what I am looking for. Thanks for your help :-)
Re: Comparing keys in 3 different hashes and if they are the same them print the values on one line
by Kenosis (Priest) on Mar 01, 2013 at 05:40 UTC

    List::Compare can be helpful here. Just send the references to those three hashes to List::Compare's get_intersection method and it'll return the keys that those hashes have in common:

    use strict; use warnings; use List::Compare; my %hash1 = ( A => 1, B => 2, C => 3, D => 4 ); my %hash2 = ( C => 30, D => 40, A => 10, B => 20 ); my %hash3 = ( F => 600, B => 200, A => 100, E => 500 ); my $lc = List::Compare->new( \%hash1, \%hash2, \%hash3 ); my @intersection = $lc->get_intersection; if (@intersection) { for my $key (@intersection) { print "\%hash1: $key => $hash1{$key}\n"; print "\%hash2: $key => $hash2{$key}\n"; print "\%hash3: $key => $hash3{$key}\n"; print "\n"; } } else { print "The three hashes don't share any common keys.\n"; }

    Output:

    %hash1: A => 1 %hash2: A => 10 %hash3: A => 100 %hash1: B => 2 %hash2: B => 20 %hash3: B => 200
Re: Comparing keys in 3 different hashes and if they are the same them print the values on one line
by Anonymous Monk on Mar 01, 2013 at 03:35 UTC

      Ok thanks I will take a look at that now. I have been trying to work out how to do this for ages but all my attempts keep failing :-(