Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Access Hashes of Hashes etc.

by 1arryb (Acolyte)
on Sep 14, 2011 at 13:43 UTC ( #925906=note: print w/ replies, xml ) Need Help??


in reply to Access Hashes of Hashes etc.

packetstormer,

DrHyde's answer is correct, if elliptical. The key concept is that hashes can only contain scalar values. That means the only way to have a hash inside a hash is to use a reference for the inner hash. And as long as you're doing that, you might as well use a reference for the outer hash itself to make the syntax more regular ($thingy->{foo}->[3] vs. $thingy{foo}->[3]).


Comment on Re: Access Hashes of Hashes etc.
Select or Download Code
Re^2: Access Hashes of Hashes etc.
by fullermd (Curate) on Sep 14, 2011 at 17:00 UTC
    And as long as you're doing that, you might as well use a reference for the outer hash itself to make the syntax more regular ($thingy->{foo}->3 vs. $thingy{foo}->3).

    Well, except the internal ->'s aren't necessary anyway, so $thingy{foo}[3] works just fine.

      True, of course, but in my opinion using the dereference operator -> explicitely anyway makes it clearer what your intentions are. $thingy{foo}[3] might work when %thingy is your root hash, so to speak. But once you're deeper into the structure, where you've loaded stuff into variables such as $thingies_a_thingy_does_in_the_weekends which would be an arrayref or hashref, you're either bound to use -> or dereference the whole thing anyway.

      That, and there's nothing wrong with littering your code with ->.

      Sometimes being explicit makes things all the more readable. For example, let's assume a function superFunc, that returns different things based on whether its called in scalar context or list context. In my $superScalar = scalar(superFunc) it's already clear that you want the scalar context behaviour from the my $superScalar part. But by calling it as scalar(superFunc) you say: "yes, I know that superFunc is context sensitive and yes, I really want its scalar context behaviour."

      But I digress.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2014-10-26 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (152 votes), past polls