sub unspace_keys { my $hash = shift; if (ref($hash) eq 'HASH') { my $out = {}; for my $k (keys %$hash) { (my $k1 = $k) =~ s/\s+//g; $out->{$k1} = unspace_keys($hash->{$k}); } return $out; } else { return $hash; } } my $new = unspace_keys( \%global ); print Dumper \%global; print Dumper $new;