in reply to Re: Problems with HOHOH modification
in thread Problems with HOHOH modification

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.

Replies are listed 'Best First'.
Re^3: Problems with HOHOH modification
by Fletch (Bishop) on Feb 13, 2008 at 19:06 UTC

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

      $HOHOH{'Alphakey2'}->{'Betakey2'} ||= {}; This was the syntax I was looking for. I havenīt seen this type of syntax before. Thank You. Is it possible to use in on something else? If var is not assigned to assign two to it.. $var||=2;

        Yes!

        || returns the value that dictated the outcome of the operation - either the left hand value if it was true, or the right hand value if the left is false. See perlop and keep in mind that Perl has useful ideas about what constitutes true and false. undef, '', '0' and 0 are all false. Consider:

        print undef || "undef is false", "\n"; print 0 || "0 is false", "\n"; print '' || "'' is false", "\n"; print '0' || "'0' is false", "\n"; print '0xxx' || "'0xxx' is false", "\n"; print 'false' || "'0xxx' is false", "\n"; print '1' || "'0xxx' is false", "\n";

        Prints:

        undef is false 0 is false '' is false '0' is false 0xxx false 1

        Perl is environmentally friendly - it saves trees
Re^3: Problems with HOHOH modification
by BrowserUk (Patriarch) on Feb 13, 2008 at 19:08 UTC

    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.