Hello Mallik, 

you asked:
> I want to accomplish some thing like this...
> 
> %hash = (
>     "abc" => "mallik",
>     "xyz" => "arjun",
>     "mno" => "priya"
> );
> 
> Need be changed to
> 
> %hash = (
>     "123" => "mallik",
>     "243" => "arjun",
>     "532" => "priya"
> );
> 
> The key value abc is changed to 123 and so on..
> 
> Hope my requirements are clear now.

If your new keys don't overlap with the old ones, you could
do it like this:

# set up a key mapping
my %mapping = ( 'abc' => '123', 'xyz' => '243', 'mno' => '532' );

my %hash = (
    "abc" => "mallik",
    "xyz" => "arjun",
    "mno" => "priya"
);

# not recommended for large hashes, but syntactically elegant
@hash{values %mapping} = @hash{keys %mapping};
delete @hash{keys %mapping};

# slightly less attractive code with a low memory overhead
foreach my $key ( keys %mapping ){
  $hash{$mapping{$key}} = $hash{$key};
  delete $hash{$key};
}

If keys worked like substr, you might've gotten away with

        keys %hash = @mapping{keys %hash};

Alas, it doesn't.

Doing the same operation for overlapping key sets is left as an
exercise for the reader ;-)

HTH,
Thomas


-- 
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