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

Re^5: Possible for Map to create Hash of Hash?

by runrig (Abbot)
on May 24, 2013 at 21:37 UTC ( #1035207=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Possible for Map to create Hash of Hash?
in thread Possible for Map to create Hash of Hash?

With the nested approach he can also use things like keys %{$hash{$store}} to list all products.

If the OP doesn't need it, then no need to do it that way. We have no way of knowing if that's what the OP needs, maybe they don't know about the other solution. It's much more memory efficient than nested hashes, so is better when you don't need the nested hashes. It's best to know the pros and cons of both ways. I used the alternate solution just the other day to store 100,000's of what would have been nested several layers deep hashes, and I did not need to use keys or values or each. I just needed a quick lookup for a group of several different values.

The delimiter approach is a nice idea, but is neither shorter to write nor more efficient.

Also, this:

$hash{$product,$code} = 1;
Is very slightly shorter than this:
$hash{$product}{$code} = 1;

And the first requires only one hash lookup, while the second requires two. Not to micro-optimize or anything though...(and I'm not going to bother benchmarking the difference between joining two strings and looking up a hash value...at least not today, anyway)


Comment on Re^5: Possible for Map to create Hash of Hash?
Select or Download Code
Re^6: Possible for Map to create Hash of Hash?
by LanX (Canon) on May 24, 2013 at 22:11 UTC
    > If the OP doesn't need it, then no need to do it that way. We have no way of knowing if that's what the OP needs, maybe they don't know about the other solution.

    Well the OP explicitly asked for HoHs and said " To create a hash that will allow me to determine if a store has a product I can use: "

    I'm no native speaker, but for me this also reads like "if a store has products at all" which is quite complicated with a flat multi-dim hash.

    I'm not banning multi-dim hashes both approaches have pro and cons.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Well the OP explicitly asked for HoHs and said " To create a hash that will allow me to determine if a store has a product I can use: "

      I'm no native speaker, but for me this also reads like "if a store has products at all" which is quite complicated with a flat multi-dim hash.

      To me it sounds like he just needs a store/product lookup and assumes that he needs an explicit HoH...but it is somewhat ambiguous, and things around here are not always clearly written, so let's just throw both solutions out there and let'em pick.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (16)
As of 2014-08-29 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (282 votes), past polls