in reply to Is it possible to use key/values of a Hash/Hash ref while it's being initialized?

no -- run it and then Dumper %hash -- you'll see that the second key is empty (or blank, depeding on what alter does). If you run with strict/warnings it will error with 'Global symbol "%hash" requires explicit package name' ...

another alternative would be: </code> my %hash = ( key1 => 'longstring' ); $hash{key2} = alter( $hash{key1} ); </code>
  • Comment on Re: Is it possible to use key/values of a Hash/Hash ref while it's being initialized?
  • Download Code

Replies are listed 'Best First'.
Re^2: Is it possible to use key/values of a Hash/Hash ref while it's being initialized?
by shiza (Hermit) on Sep 14, 2005 at 20:04 UTC
    Yes, I was aware of that. I didn't know if there was some backdoor/hack/trick to access the current hash being initialized. Thanks for the feedback!
      the current hash being initialized
      That hash is necessarily empty until the entire value on the right (the map) is completely computed. You can't start shoving values into there because you can't alter the existing data, or else things like this woudn't work:
      @a = map { $a[$_] > 1 ? $a[$_] * 2 : () } 0..$#a;
      In this case, you can't start altering @a during the map, because @a might be getting shorter, and then you wouldn't have the right values on the right.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.