in reply to Printing from a hash table
foreach my $name ( keys %birthdays) {
if (exists $birthdays{$name} ) {
...
}
}
If you're iterating over the keys of a hash (as in the quoted code above), each key must exist in the hash! One way to do something like what you want:
c:\@Work\Perl\monks>perl -wMstrict -e "my %birthdays = ( Fred => 'June 1', Ray => 'May 31', Glenn => 'May 27', Bob => 'August 28', Jim => '', Paul => undef, ); ;; my $fmt = ' %-10s %s'; printf qq{$fmt \n}, 'Name', 'Birthday'; print '-' x 25, qq{\n}; ;; B_DAY: foreach my $name (keys %birthdays) { next B_DAY unless defined $birthdays{$name} and length $birthdays{$name} ; printf qq{$fmt \n}, $name, $birthdays{$name}; } " Name Birthday ------------------------- Ray May 31 Bob August 28 Glenn May 27 Fred June 1
Give a man a fish: <%-{-{-{-<
|
---|