in reply to looping through a hash and looking up values from another hash

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.

Replies are listed 'Best First'.
Re^2: looping through a hash and looking up values from another hash
by pearllearner315 (Acolyte) on Apr 26, 2017 at 03:25 UTC
    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