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

hi, i have a hash of arrays where i want to change the name of the key. How would I access the key and then change its name. i am trying
$b="blah"; $hasharr{$rephead}=$b; #but i think i am accessing the array rather than the key.
thanks.

Replies are listed 'Best First'.
Re: modifying the key of hash of arrays
by davido (Cardinal) on Oct 16, 2003 at 07:56 UTC
    Keys themselves are immutable. However, you can assign the value held in a hash element to a new key and delete the old one.

    "I am trying:

    $b="blah"; $hasharr{$rephead}=$b; #but i think i am accessing the array rather than the key.

    Yes, you are writing "blah" into the value of the hash key/value pair, overwriting the anonymous array you had stored there.

    Here is what you need to do:

    $hasharr{$newkey} = $hasharr{$rephead}; delete $hasharr{$rephead};


    Dave


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      delete returns the value of the key it deleted, so you actually only need:

      $hasharr{$newkey} = delete $hasharr{$rephead};

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      :(){ :|:&};:

      Note: All code is untested, unless otherwise stated