# %food_color as per Introduction
sub print_foods {
my @foods = keys %food_color;
my $food;
print "Keys: @foods\n";
print "Values: ";
foreach $food (@foods) {
my $color = $food_color{$food};
if (defined $color) {
print "$color ";
} else {
print "(undef) ";
}
}
print "\n";
}
print "Initially:\n";
print_foods( );
print "\nWith Banana undef\n";
undef $food_color{"Banana"};
print_foods( );
print "\nWith Banana deleted\n";
delete $food_color{"Banana"};
print_foods( );
Initially:
Keys: Banana Apple Carrot Lemon
Values: yellow red orange yellow
With Banana undef
Keys: Banana Apple Carrot Lemon
Values: (undef) red orange yellow
With Banana deleted
Keys: Apple Carrot Lemon
Values: red orange yellow
As you see, if we set $food_color{"Banana"} to
undef, "Banana" still shows up
as a key in the hash. The entry is still there; we only succeeded in
making the value undef. On the other hand,
delete actually removed it from the
hash—"Banana" is no longer in the list
returned by keys.
delete can also take a hash slice, deleting all
listed keys at once:
delete @food_color{"Banana", "Apple", "Cabbage"};