Octavian Rasnita wrote:
Hi all,

Please tell me how to convert the elements of a hash to uppercase, if I can
access that hash only by reference.

For example, I have something like:

my $ref = { 'a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc' };

And I want to make 'a' to be 'A', 'b' to be 'B' and so on, but the values of
the hash need to remain the same.

I have tried using map() but without success.

You can't modify the key; you can only create a new one and remove the old one:


#!/usr/bin/perl;

use strict;
use warnings;

my $ref = { 'a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc' };

while ( my($k,$v) = each %$ref ) {
    $ref->{uc($k)} = delete( $ref->{$k} );
}

use Data::Dumper;
print Dumper( $ref );

__END__

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to