Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

changing the hash key as upper case.

by santhosh_89 (Scribe)
on Sep 17, 2009 at 11:48 UTC ( #795862=perlquestion: print w/ replies, xml ) Need Help??
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));

Comment on changing the hash key as upper case.
Download Code
Re: changing the hash key as upper case.
by moritz (Cardinal) on Sep 17, 2009 at 12:03 UTC
    If it should work in arbitrary depth, you need to recurse into the same function. Like this:
    #! /usr/bin/perl use strict; use warnings; use Data::Dumper; use Scalar::Util qw(reftype); sub Hash_Key_Cap { my $entry=shift; # reftype() my return undef, don't warn about that: no warnings 'uninitialized'; if (reftype($entry) eq 'HASH') { my %h = map { uc $_ => Hash_Key_Cap($entry->{$_}) } keys %$ent +ry; return \%h; } elsif (reftype($entry) eq 'ARRAY') { return [ map Hash_Key_Cap($_), @$entry ]; } else { 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));

    Note that proper indention is essential for the readability of the code.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: changing the hash key as upper case.
by AnomalousMonk (Abbot) on Sep 17, 2009 at 16:48 UTC
    Update: I just went back and checked the OP again and finally noticed that, because the OP code assigns to the hash from the output of a  map statement, the code is unquestionably changing all the keys! Consequently, this entire comment is misconceived and should be ignored. Rats. Nevermind...

    Nodereaper, please bury my shame.

    ... requirement ... to change the hash key [to] upper case...
    It is not clear to me if this means add an all-upper-case copy of any key that contains any lower case character or if it means add an all-upper-case copy of any key that contains any lower case character and delete the original key. The code of the OP certainly does the former, but, for reasons not clearly stated, that code is unacceptable.

    If the latter of the two alternatives given above is needed, the following variation will do the trick. (The first hash operation is shown for demonstration purposes; only the second operation is needed for add-and-delete, i.e., change.)

    >perl -wMstrict -MData::Dumper -le "my $hr = { qw(a 1 b 2 c 3 d 4) }; print Dumper $hr; $hr->{ uc() } = $hr->{$_} for keys %$hr; print Dumper $hr; $hr->{ uc() } = delete $hr->{$_} for keys %$hr; print Dumper $hr; " $VAR1 = { 'c' => '3', 'a' => '1', 'b' => '2', 'd' => '4' }; $VAR1 = { 'A' => '1', 'a' => '1', 'd' => '4', 'B' => '2', 'c' => '3', 'b' => '2', 'C' => '3', 'D' => '4' }; $VAR1 = { 'A' => '1', 'B' => '2', 'D' => '4', 'C' => '3' };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://795862]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-21 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls