The hash looks something like: $global{'title'}='Report for today' $global{'data 1'}{title}='My data 1 title' $global{'data 1'}{data}= (ref to hash) $global{'data 1'}{footer}='My footer' $global{'data 2'}{title}='My data 2 title' $global{'data 2'}{subtitle}='Data 2 subtitle' $global{'data 2'}{data}= (ref to hash) $global{'data 2'}{'data percentages'}= (ref to hash) $global{'data 2'}{'data raw'}= (ref to hash) $global{'data 2'}{footer}='Footer for data 2' and so on.. #### sub hash_remove_key_space { my $in_hash = shift; my %work_hash = %$in_hash; my %output_hash; foreach my $ckey(keys %work_hash){ my $value; if (ref($work_hash{$ckey}) eq "HASH") { $value = hash_remove_key_space($work_hash{$ckey}); } else { $value = $work_hash{$ckey}; } my $temp = $ckey; $temp =~ s|\s||isg; $output_hash{$temp} = $value; } return \%output_hash; } corrected code - per Sam