use strict; use warnings; use subs qw( version1 version2 ); my @delete = qw(bar baz); my %hash = (foo => 1, bar => 2, baz => 3); print "Keys in the hash: ", join(", ", keys %hash), "\n"; print "Version 1 reports ", version1, " as the sum of values deleted from the hash.\n"; print "Keys in the hash after version 1: ", join(", ", keys %hash), "\n"; print "\n"; %hash = (foo => 1, bar => 2, baz => 3); print "Keys in the hash: ", join(", ", keys %hash), "\n"; print "Version 2 reports ", version2, " as the sum of values deleted from the hash.\n"; print "Keys in the hash after version 2: ", join(", ", keys %hash), "\n"; sub version1 { my $num_deleted = 0; for (@delete) { my $deleted = delete $hash{$_}; $num_deleted += $deleted if $deleted; } return $num_deleted } sub version2 { my $num_deleted = 0; for (@delete) { $num_deleted += delete $hash{$_} if delete $hash{$_} } return $num_deleted } __END__ Keys in the hash: bar, baz, foo Version 1 reports 5 as the sum of values deleted from the has. Keys in the hash after version 1: foo Keys in the hash: bar, baz, foo Use of uninitialized value in addition (+) at G:\y.pl line 35. Use of uninitialized value in addition (+) at G:\y.pl line 35. Version 2 reports 0 as the sum of values deleted from the hash. Keys in the hash after version 2: foo