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'.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|