#!/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); }