I don't see what's wrong with yours. I found it easier to write my own.

use strict; use warnings; use Data::Dumper; my %global; $global{'title'}='Report for today'; $global{'data 1'}{title}='My data 1 title'; $global{'data 1'}{data}= {}; $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}= {}; $global{'data 2'}{'data percentages'}= {}; $global{'data 2'}{'data raw'}= {}; $global{'data 2'}{footer}='Footer for data 2'; unspace_keys( \%global ); print Dumper \%global; sub unspace_keys { my $hash_ref = shift; foreach my $spaced_key ( grep /\s/, keys %{ $hash_ref } ) { ( my $new_key = $spaced_key ) =~ s/\s+//g; if ( exists $hash_ref->{$new_key} ) { die "unspaced key '$new_key' exists"; } $hash_ref->{$new_key} = $hash_ref->{$spaced_key}; delete $hash_ref->{$spaced_key}; } unspace_keys( $_ ) for grep { ref $_ eq ref {} } values %{ $hash_ref }; return; } __END__ $VAR1 = { 'data2' => { 'subtitle' => 'Data 2 subtitle', 'data' => {}, 'title' => 'My data 2 title', 'dataraw' => {}, 'datapercentages' => {}, 'footer' => 'Footer for data 2' }, 'title' => 'Report for today', 'data1' => { 'data' => {}, 'title' => 'My data 1 title', 'footer' => 'My footer' } };

This is (also) a recursive solution. It does not check for cycles, so it will need some work if your input has any. It does check if a new unspaced key already exists, so it won't clobber anything if you have keys like 'psychotherapist' and 'psycho therapist'.


In reply to Re: removing the spaces from keys of a complex hash by kyle
in thread removing the spaces from keys of a complex hash by gwhite

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.