You just go ahead and try it from the command line (make sure that warnings are enabled):
But you have to be careful, because sometimes perl will conjure keys from thin air. For example:% perl -wle 'my %foo; if ( %foo ) { print "OK" } else { print "KO" }' KO % perl -wle 'my %foo = (1,2); if ( %foo ) { print "OK" } else { print +"KO" }' OK
Notice how the second test succeeded, even though we never initialized %foo. For this reason, maybe something like this (also proposed by brian_d_foy) would be better:% perl -wl print( %foo ? "OK" : "KO" ); my @bar = grep $_, @foo{ 1, 2, 3 }; print( %foo ? "OK" : "KO" ); ^D KO OK
This test will succeed only if %foo contains at least one defined value.if ( grep defined, values %foo ) { # etc. }
the lowliest monk
In reply to Re: checking to see if a hash has data
by tlm
in thread checking to see if a hash has data
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |