santhosh_89 has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks I have a requirement i wanted to change the hash key as upper case,I will pass any type of perl structure(hash reference),I have tried an example program for that.I could provide the result.But I was not able to update the recursive hash operation.can anyone update my query,It should be efficient.
#! /usr/bin/perl use strict; use warnings; use Data::Dumper; sub Hash_Key_Cap { my $entry=shift; %$entry = map { uc $_ => $entry->{$_} } keys % +$entry; foreach my $main ( sort keys %$entry){ if( ref $entry->{$main} eq "HASH" ) { %{ $entry->{ $main } } = map { uc $_ => $entry +->{ $main }{ $_ } } keys %{ $entry->{ $main } }; } elsif(ref $entry->{$main} eq "ARRAY" ){ foreach my $test (@{$entry->{$main}}){ if (ref $test eq "HASH") { %{$test} = map { uc $_ => $te +st->{ $_ } } keys %{$test} }; } } } return $entry; } my %hash=( 'a'=>'Wow', 'b'=>['GOOD', 'BAD'], 'c'=>[ {one=>'perl', two=>'monks', five=>{a=>'b',c=>{ c=>'d' } }, three=>'Test', four=>'Best' }, 2], 'd'=> { five=>5,size=>6} ); print Dumper(Hash_Key_Cap(\%hash));
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: changing the hash key as upper case.
by moritz (Cardinal) on Sep 17, 2009 at 12:03 UTC | |
|
Re: changing the hash key as upper case.
by AnomalousMonk (Archbishop) on Sep 17, 2009 at 16:48 UTC |