pearllearner315 has asked for the wisdom of the Perl Monks concerning the following question:
%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 3as 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 | |
|
Re: looping through a hash and looking up values from another hash
by huck (Prior) on Apr 26, 2017 at 03:06 UTC | |
|
Re: looping through a hash and looking up values from another hash
by 1nickt (Canon) on Apr 26, 2017 at 03:08 UTC | |
by pearllearner315 (Acolyte) on Apr 26, 2017 at 03:25 UTC | |
by huck (Prior) on Apr 26, 2017 at 03:31 UTC | |
|
Re: looping through a hash and looking up values from another hash
by BillKSmith (Monsignor) on Apr 26, 2017 at 16:18 UTC | |
|
Re: looping through a hash and looking up values from another hash
by Anonymous Monk on Apr 26, 2017 at 03:02 UTC |