Print the value in Hash of Hash for two different keys in Perl -



Print the value in Hash of Hash for two different keys in Perl -

below hash in perl:

my %hash = ( 'episode1' => { 'when' => '08.13.97', 'airdate' => '08.13.97', 'episodenumber' => '101', 'id' => '103511', 'title' => 'cartman gets anal probe', 'available' => 'true' }, 'episode2' => { 'when' => '08.20.97', 'airdate' => '08.20.97', 'episodenumber' => '102', 'id' => '1035156', 'title' => 'weight gain 4000', 'available' => 'true' } );

i want print "id" of both episodes,but below code not working:

foreach $key1 ( keys %hash ) { foreach $key2 ( keys %{$hash{$key1}} ) { print "$hash{$key1}{$key2}{id}\n"; } }

please help.

try this:

foreach $key1 ( keys %hash ) { print "$hash{$key1}{id}\n"; }

or

foreach $key1 ( keys %hash ) { foreach $key2 ( keys %{$hash{$key1}} ) { print "$hash{$key1}{$key2}\n" if($key2 eq 'id'); } }

perl hash

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -