Convert multilevel hash in simple hash in perl -
i have hash multilevel hash want convert single level .but not able that
actual hash:
'mainslab' => { 'a1' => { 'slab' => { '49_168' => { 'amt' => '3000', 'start' => '49', 'end' => '168' }, '169_720' => { 'amt' => '3000', 'start' => '169', 'end' => '720' }, '2_48' => { 'amt' => '3000', 'start' => '2', 'end' => '48' }, '721_-' => { 'amt' => '3000', 'start' => '721', 'end' => '-' } } }, 'a2' => { 'slab' => { '49_168' => { 'amt' => '3000', 'start' => '49', 'end' => '168' }, '169_720' => { 'amt' => '4000', 'start' => '169', 'end' => '720' }, '2_48' => { 'amt' => '5000', 'start' => '2', 'end' => '48' }, '721_-' => { 'amt' => '3000', 'start' => '721', 'end' => '-' } } } }
i want convert simple , single level hash :
slab =>{ "49_168"=>{"a1"=> "3000","a2"=>"3000"}, "169_720"=>{"a1"=>"4000","a2"=>"4000"}, "2_48"=>{"a1"=>"5000","a2"=>"5000"}, "721_"=>{"a1"=>"3000","a2"=>"3000"} }
please me how can this
assuming
my %hash = ( 'mainslab' => { 'a1' => { 'slab' => { '49_168' => { 'amt' => '3000', 'start' => '49', 'end' => '168' }, 'a2' => ... );
then:
my $hashref = $hash{'mainslab'}; $new_hashref = {}; foreach $ax (keys %$hashref) { foreach $k (keys %{$hashref->{$ax}{'slab'}}) { $new_hashref->{$k}{$ax} = $hashref->{$ax}{'slab'}{$k}{'amt'}; } } %new_hash = (slab => $new_hashref);
will produce:
$new_hash = ( 'slab' => { '49_168' => { 'a1' => '3000', 'a2' => '3000' }, '169_720' => { 'a1' => '3000', 'a2' => '4000' ... );
Comments
Post a Comment