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

Hello Monks, I have a hash with numbers as the keys and an array of elements as the values. I also have another hash with the numbers as the keys (these numbers exist in the first hash but numbers in first hash may not exist in second hash) and a number count as the values.
%hash1 = ( 1=> ["You", "Me", "Him", "Her"], 2 => ["You", "Me", "Him", + "Her"], 3 => ["You", "Me", "Him", "Her"], and so on). %hash2 = ( 1 => 3, 51 => 0, 32 => 1, and so on)

I need to loop through hash1, print out its values and also look up the value from the second hash that corresponds to the same key in the first hash and print that value. For example:

If one of the keys from hash1 was 3 and if 3 existed in hash2, you would print the value of 3 from hash1 and the value of 3 in hash2 on the same line.

this is what I have been doing:
foreach my $keys (keys %hash1){ if (defined $hash2{$keys}){ print @{ $hash1{$keys} }, "\t", $hash2{$keys}, "\n";

and it hasn't been printing anything. I hope to get:

You Me Him Her 3

as an example output for the first line

what is wrong with my loop? any help would be great. thanks a lot guys!

Replies are listed 'Best First'.
Re: looping through a hash and looking up values from another hash
by AnomalousMonk (Archbishop) on Apr 26, 2017 at 04:11 UTC

    I would use exists rather than defined to check for the existence of a hash key, but either way, I get the same thing everyone else gets:

    c:\@Work\Perl\monks>perl -wMstrict -le "my %hash1 = ( 1 => [ 'You', 'Me', 'Him', 'Her' ], 2 => [ 'You', 'Me', 'Him', 'Her' ], 3 => [ 'You', 'Me', 'Him', 'Her' ], ); my %hash2 = (1 => 3, 51 => 0, 32 => 1); ;; foreach my $k (keys %hash1) { if (exists $hash2{$k}) { print qq{@{ $hash1{$k} }, '$hash2{$k}'}; } } " You Me Him Her, '3'


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

Re: looping through a hash and looking up values from another hash
by huck (Prior) on Apr 26, 2017 at 03:06 UTC

    defined $hash2{keys} KEYS, i suspect you want defined $hash2{$keys} instead.

    I also suspect you want

    print join(' ',@{ $hash1{$keys} }), "\t", $hash2{$keys}, "\n";
    Instead too.

Re: looping through a hash and looking up values from another hash
by 1nickt (Canon) on Apr 26, 2017 at 03:08 UTC

    what is wrong with my loop?

    You are missing the sigil and searching for a key with the hard-coded value of 'keys'.

    if (defined $hash2{keys}){ ^

    Try this:

    for my $key ( sort keys %h1 ) { say sprintf("%s: %s\t%s", $key, "@{ $h1{ $key } }", $h2{ $key }) i +f defined $h2{ $key }; }

    Hope this helps!


    The way forward always starts with a minimal test.
      i've fixed the sigil issue but still doesn't print anything..it should print..

        use strict; use warnings; my %hash1 = ( 1=> ["You", "Me", "Him", "Her"], 2 => ["You", "Me", "Hi +m", "Her"], 3 => ["You", "Me", "Him", "Her"]); my %hash2 = ( 1 => 3, 51 => 0, 32 => 1) ; foreach my $keys (keys %hash1){ if (defined $hash2{$keys}){ print join(' ',@{ $hash1{$keys} }), "\t", $hash2{$key +s}, "\n"; } }
        Works for me
        You Me Him Her 3

Re: looping through a hash and looking up values from another hash
by BillKSmith (Monsignor) on Apr 26, 2017 at 16:18 UTC
    This approach handles all the lines.
    use strict; use warnings; my %hash1 = ( 1 => [qw( You Me Him Her )], 2 => [qw( fie fii foo fum )], 3 => [qw( aaa bbb ccc ddd )], ); my %hash2 = ( 1 => 3, 51 => 0, 32 => 1, ); foreach (sort keys %hash1){ local( $,, $\) = ("\t", "\n"); print @{ $hash1{$_} }, exists $hash2{$_} ? $hash2{$_} : q(); } OUTPUT: You Me Him Her 3 fie fii foo fum aaa bbb ccc ddd
    Bill
Re: looping through a hash and looking up values from another hash
by Anonymous Monk on Apr 26, 2017 at 03:02 UTC

    what is wrong with my loop? any help would be great. thanks a lot guys!

    Well one thing is code is incomplete it does not compile -- but you probably knew thats what you posted :/