http://www.perlmonks.org?node_id=1025459


in reply to Delete space at the end of a hash element

My understanding is that you want to remove spaces from the keys of a hash. I think you need to remove the entry from the hash and create a new one.

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;

Your proposal would not run but giving the error message:

delete argument is not a HASH or ARRAY element or slice at hashdel.pl +line 5.

This is because delete removes an entry from a hash but does not modify the key or the value.