my %hash = ( "apples " => 4, "oranges " => 5 ); foreach my $words (keys %hash) { my $elem = $hash{$words}; delete $hash{$words}; $words =~ s/\s*$//; $hash{$words} = $elem; } print %hash; #### delete argument is not a HASH or ARRAY element or slice at hashdel.pl line 5.