satzbu has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: how can i replace a hash key with other hash value?
by cdarke (Prior) on Mar 24, 2010 at 09:14 UTC
    I don't really understand your question, but this:
    %hash = { 'chapter' => ...};
    is wrong. { } returns a reference to a hash, which is a scalar. Either use ( ) or change %hash to $hash.
Re: how can i replace a hash key with other hash value?
by Utilitarian (Vicar) on Mar 24, 2010 at 10:20 UTC
    A quick example to show the method required
    use strict; use warnings; my %hash=("one" =>1, "two"=>,2); my %hash_tr_fr=(one=>"une","two"=>"deux"); for my $eng (keys %hash){ $hash{$hash_tr_fr{$eng}}=$hash{$eng}; delete $hash{$eng}; } use Data::Dumper; print Dumper(\%hash);
    You can't "replace a key" but you can set a new one and delete the existing one

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      You can't "replace a key" but you can set a new one and delete the existing one
      And you can do that in one line:
      $hash{$hash_tr_fr{$eng}} = delete $hash{$eng};
      which has the added benefit to even work correctly when $hash_tr_fr{$eng} eq $eng. Doing the delete in a separate step would result in a loss of the entry.
        ...or even in a slice without the loop. So:
        for my $eng (keys %hash){ $hash{$hash_tr_fr{$eng}}=$hash{$eng}; delete $hash{$eng}; }
        becomes:
        my @temp = keys %hash; @hash{@hash_tr_fr{@temp}} = delete @hash{@temp};
        Update: corrected missing @, thanks SuicideJunkie
Re: how can i replace a hash key with other hash value?
by almut (Canon) on Mar 24, 2010 at 09:09 UTC
    %hash = { 'chapter' => { 'subchap1' => { 'p' => 'sathish is a good bou', 'content' => 'sathish is good boy + or not ' }, 'content' => 'sathish ' } }; %hash1 = { 'chapter' => { 'subchap1' => { 'p' => 'para', 'content' => 'body ' }, 'content' => 'head ' } }; i want the output as shown below %hash= { 'head'=> { 'body' => { 'para' => 'sathish is a good bou' +, 'content' => 'sathish is good boy + or not ' }, 'content' => 'sathish ' } };

    I'm afraid I don't understand your replace logic.  If p is supposed to be replaced with para (which I presume is because of 'p' => 'para' in hash1), why isn't content meant to be replaced with body, but rather subchap1 with body?

    A reply falls below the community's threshold of quality. You may see it by logging in.