#!/usr/bin/perl use warnings; use strict; use Data::Dumper; { my %hash = ( 1 => 5, 3 => 12, 7 => 19, 9 => 44 ); print 'At ' . __LINE__ . ' there are ' . ( scalar keys %hash ) . " keys: "; print Dumper(\%hash); my %hash2 = ( 1 => 5, 9 => 22 ); print "This should delete one of the keys, using this hash ..\n"; print Dumper ( \%hash2 ); foreach my $key ( keys %hash2 ) { if ( exists $hash{ $key } ) { $hash{ $key } -= $hash2{ $key }; if ( $hash{ $key } <= 0 ) { delete $hash{ $key }; } } } print 'At ' . __LINE__ . ' there are ' . ( scalar keys %hash ) . " keys: "; print Dumper(\%hash); } #### tab@music4:~/Pianoforte/Development/Perlmonks/11113721 $ perl hashkeys.pl At 11 there are 4 keys: $VAR1 = { '9' => 44, '1' => 5, '3' => 12, '7' => 19 }; This should delete one of the keys, using this hash .. $VAR1 = { '1' => 5, '9' => 22 }; At 33 there are 3 keys: $VAR1 = { '9' => 22, '3' => 12, '7' => 19 }; tab@music4:~/Pianoforte/Development/Perlmonks/11113721 $