Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Best Hash Practices?

by DamianKaelGreen (Acolyte)
on Oct 12, 2009 at 17:51 UTC ( #800761=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: Best Hash Practices?
Download Code
Re^3: Best Hash Practices? (tie)
by LanX (Canon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://800761]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-12-19 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (78 votes), past polls