http://www.perlmonks.org?node_id=466851

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

Hi,
With this code
#!/usr/bin/perl -w use strict; use Data::Dumper; my $hoh = { 'pk1' => { 'sk1' => [ 'val1', 'val2' ], 'sk2' => { 'tk1' => 'val1 val2 ', 'tk2' => 'val9,val7' }, 'sk3' => 'val10 val11', 'sk4' => 21 }, 'pk2' => {}, }; #print Dumper $hoh; foreach my $k ( keys %{$hoh} ) { if( $hoh->{$k} ) { print "$k\n"; }; }
How can I make it only print "pk1" since "pk2" has no value in it, without having to go through second loop. What's wrong with my test above? Now it prints both "pk1 and pk2"