It can be done as follows:-
#!/usr/bin/perl -w
use strict;
use warnings;
my @arry=(18,96,117,336.9);
my %old_hash=(
a => {
aa => {
aaa => "aaa_value",
aab => "aab_value",
aac => "aac_value",
aad => "aad_value",
},
ab => {
aba => "aba_value",
abb => "abb_value",
abc => "abc_value",
abd => "abd_value",
},
ac => {
aca => "aca_value",
acb => "acb_value",
acc => "acc_value"
},
ad => \@arry
},
b => {
ba => {
baa => "baa_value",
bab => "bab_value",
bac => "bac_value",
bad => "bad_value",
},
bb => {
bba => "bba_value",
bbb => "bbb_value",
bbc => "bbc_value",
bbd => "bbd_value",
},
bc => {
bca => "bca_value",
bcb => "bcb_value",
bcc => "bcc_value"
},
db => [5,8,69,99]
}
);
my %hash=%old_hash;
my $thash=&process_hash(\%hash);
my %new_hash=%{$thash};
use Data::Dumper;
print "Original\n".Dumper(\%old_hash);
print qq{\n\n------------------------------------------------------------------------------\n\n};
print "Modified\n".Dumper(\%new_hash);
sub process_hash {
my ($temp_hash)=@_;
if(&is_hash($temp_hash)) {
foreach my $key(keys %{$temp_hash}) {
if(&is_hash($temp_hash->{$key})) {
my %t=%{$temp_hash->{$key}};
$temp_hash->{$key}=&process_hash(\%t);
} elsif(&is_array($temp_hash->{$key})) {
my @t=@{$temp_hash->{$key}};
$temp_hash->{$key}=&process_hash(\@t);
} else {
if($temp_hash->{$key} && $temp_hash->{$key} ne '') {
$temp_hash->{$key}="--".$temp_hash->{$key}."--";
}
}
}
} elsif(&is_array($temp_hash)) {
my $x=0;
foreach my $values(@$temp_hash) {
if(&is_array($values)) {
my @t=@{$values};
$temp_hash->[$x]=&process_hash(\@t);
} elsif(&is_hash($values)) {
my %t=%{$values};
$temp_hash->[$x]=&process_hash(\%t);
} else {
$temp_hash->[$x]="--".$values."--";
}
$x++;
}
}
return $temp_hash;
}
sub is_hash {
my ($ref) = @_;
return 0 unless ref $ref;
if ( $ref =~ /^HASH/ ) {
return 1;
} else {
return 0;
}
}
sub is_array {
my ($ref) = @_;
return 0 unless ref $ref;
eval {
my $a = @$ref;
};
if ($@=~/^Not an ARRAY reference/) {
return 0;
} elsif ($@) {
die "Unexpected error in eval: $@\n";
} else {
return 1;
}
}
Output:-
Original
$VAR1 = {
'a' => {
'ab' => {
'abb' => 'abb_value',
'abc' => 'abc_value',
'aba' => 'aba_value',
'abd' => 'abd_value'
},
'ad' => [
18,
96,
117,
'336.9'
],
'ac' => {
'acb' => 'acb_value',
'aca' => 'aca_value',
'acc' => 'acc_value'
},
'aa' => {
'aaa' => 'aaa_value',
'aad' => 'aad_value',
'aac' => 'aac_value',
'aab' => 'aab_value'
}
},
'b' => {
'db' => [
5,
8,
69,
99
],
'ba' => {
'bad' => 'bad_value',
'baa' => 'baa_value',
'bab' => 'bab_value',
'bac' => 'bac_value'
},
'bb' => {
'bbc' => 'bbc_value',
'bbb' => 'bbb_value',
'bbd' => 'bbd_value',
'bba' => 'bba_value'
},
'bc' => {
'bcc' => 'bcc_value',
'bca' => 'bca_value',
'bcb' => 'bcb_value'
}
}
};
------------------------------------------------------------------------------
Modified
$VAR1 = {
'a' => {
'ab' => {
'abb' => '--abb_value--',
'abc' => '--abc_value--',
'aba' => '--aba_value--',
'abd' => '--abd_value--'
},
'ad' => [
'--18--',
'--96--',
'--117--',
'--336.9--'
],
'ac' => {
'acb' => '--acb_value--',
'aca' => '--aca_value--',
'acc' => '--acc_value--'
},
'aa' => {
'aaa' => '--aaa_value--',
'aac' => '--aac_value--',
'aad' => '--aad_value--',
'aab' => '--aab_value--'
}
},
'b' => {
'ba' => {
'baa' => '--baa_value--',
'bad' => '--bad_value--',
'bab' => '--bab_value--',
'bac' => '--bac_value--'
},
'db' => [
'--5--',
'--8--',
'--69--',
'--99--'
],
'bb' => {
'bbb' => '--bbb_value--',
'bbc' => '--bbc_value--',
'bba' => '--bba_value--',
'bbd' => '--bbd_value--'
},
'bc' => {
'bcc' => '--bcc_value--',
'bca' => '--bca_value--',
'bcb' => '--bcb_value--'
}
}
};
Like this:
Like Loading...
You must be logged in to post a comment.