my %newhash; while (my ($k, $v) = each( %hash)) { $k =~ s/\s+$//; $newhash{$k} = $v; } # newhash contains correct key values