http://www.perlmonks.org?node_id=800761


in reply to Re: Best Hash Practices?
in thread Best Hash Practices?

Yes, this is the direction I was headed with this too... It would be nicer though if the input to the function was just the autovivified list of keys though, so we could maintain a more usual appearance, like:

print 6, isDefined($h{"a"}{2}{c}{4});

This is a kind of standard function that I think should be developed more thoroughly for everyone to use regularly; Either that, or the fundamentals of the way a multidimensional hash is handled should possibly be re-evaluated to recognize when something is being created vs when it's being tested...

Replies are listed 'Best First'.
Re^3: Best Hash Practices? (tie)
by LanX (Saint) on Oct 12, 2009 at 20:14 UTC
    Hi

    I know what you mean but the syntax you're proposing is not feasible, since it's the arrow operator which is autovivifying!

    isDefined($h{"a"}->{2}->{c}->{4});

    So when isDefined comes into action it's already to late!

    Anyway one can easily extend the syntax of my approach to make it "smoother"!

    xdefined(%h => qw/a 2 c 4/)

    or

    xdefined(%h => "a",2,"c",4)

    But IMHO (within the boundaries of perl) the syntactically "prettiest" way to go would be to use a tied copy of your hash, like this you may be able to write

    novivi(%h)->{a}{2}{c}{4}

    %h2=novivi(%h) should be an empty hash with a hash-tie which mirrors safely the structure of %h.

    Such that $h2{a} is only be defined iff $h{a} is defined and all the automatically vivified structures in $h2{a}{2}{c}{4} are automatically tied hashes bound to the corresponding structures in %h.

    Well ... I think this is feasible, BUT should come with a significant performance penalty compared to my first approach, since ties are expensive.

    Cheers Rolf