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

Suppose I have a hash of hashes of hashes:

my %HOHOH;
..I could add some vaules like this:

$HOHOH{'Alphakey1'}{'Betakey1'}{'Gammakey1'}=1; $HOHOH{'Alphakey1'}{'Betakey1'}{'Gammakey2'}=2;
Then the structure would now look like:

Alphakey1 { =>Betakey1 { ¨ { =>Gammakey1=1 =>Gammakey1=2 } } }
But how do I do to add Alphakeys or Betakeys only? How should I do for example add a "Alphakey2" that is a key to "Betakey2". Where "Betakey2" Has no gammakeys? Thank you for any help.

Replies are listed 'Best First'.
Re: Problems with HOHOH modification
by BrowserUk (Patriarch) on Feb 13, 2008 at 18:18 UTC
    How should I do for example add a "Alphakey2" that is a key to "Betakey2". Where "Betakey2" Has no gammakeys?
    $HOHOH{ Betakey2 }{ Alphakey2 } = undef;

    I strongly suspect that you mean something different to what you asked for, but the above is what you asked for.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Sorry that my previous explenation was not strict enough. Undef is not thing I need becouse it would undefine values that have allredy have been defined. I would for example like to create the following structure, by adding keys "Alphakey2" and "Betakey2".
      Alphakey1 { =>Betakey1 { ¨ { =>Gammakey1=1 =>Gammakey1=2 } } } Alphakey2 { =>Betakey2 { } }
      If "Alphakey2" and "Betakey2" are not allready defined. If there allredy are keys named "Alphakey2" and "Betakey2" there should be no changes.

        $HOHOH{'Alphakey2'}->{'Betakey2'} ||= {}; will initialize with a new (empty) hashref if there's not already an existing key at that level (although keep in mind that that will also populate $HOHOH{'Alphakey2'} with a new hashref as well if it doesn't exist; search for "autovivification" for more details, and go read perlreftut and perlref as well until it becomes clear).

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        You want $HOHOH{ Betakey2 }{ Alphakey2 } to contain an empty hash? Then assign it an empty hash:

        $HOHOH{ Betakey2 }{ Alphakey2 } = {};

        But again, I suspect that you are not describing your requirements very well? For example, what happened to the top level hash in youe examples? What you have posted looks like two entirely different data structures.

        I'm not trying to offend you, but whilst your requirement may be entirely obvious to you, it is far from clear to us reading your post.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Problems with HOHOH modification
by pc88mxer (Vicar) on Feb 13, 2008 at 18:17 UTC
    How about:

    $HOHOH{'Alphakey2'}{'Betakey2'} = $value;

    However, I have a feeling this is not what you want to do. For instance, this precludes you later from setting a value for 'Alphakey2', 'Betakey2', 'Gammakey2'. If that's what you want to do, you'll probably want to use the comma operator like this:

    $hash{$alpha,$beta,$gamma} = $value;

    The comma operator here is syntactic sugar for "$alpha$;$beta$;$gamma", so you are only creating one hash but with a special key. To not include a gamma component, just omit it:

    $hash{$alpha,$beta} = $value;

    Update: It just occurred to me that maybe you want to do this:

    $HOHOH{'Alphakey2'}{'Betakey2'}{undef} = $value

    which preserves the HOHOH structure.