Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Tied multidimensional hashes

by shmem (Canon)
on Oct 13, 2006 at 11:33 UTC ( #578116=note: print w/ replies, xml ) Need Help??


in reply to Tied multidimensional hashes

A bit debugging like so

sub STORE { warn "STORE: ".join(', ',map{"'$_'"} @_)."\n"; ...

reveals

STORE: 'FlatHash=ARRAY(0x820fd58)', 'Monkey', 'HASH(0x820fd88)'

at the first store attempt - your $_[2] isn't what you expect. What is happening here?

Storing the value "Eating", a FETCH is done first, which you don't see, because Tie::Hash does this for you. Since FETCH doesn't return anything, an anonymous hash is autovivified and passed into STORE as the value. This value is stored under the (lowercased) key 'monkey'. Done with the tied hash. The autovivified anonymous hash is not tied. This hash is thereafter populated with the key/value pair (habits,Eating).

At the second assignment, the value stored under the key 'monkey' is retrieved - the anonymous hash, which is not tied, and then a normal store operation is done: the value 'Eating' in that anonymous hash is overwritten with 'Scratching'. The tied hash is involved only at FETCH.

Did you read notice the 'FETCH' sub in Re: How to tie multilevel (multidimensional) hash?? It takes care that the value retrieved is of the right type (a tied hash), so that autovivification does not happen.

Ultimately I'd like the final datastructure to be something like:
$VAR1 = { 'monkey' => { 'habits' => ['Eating', 'Scratching'] } };

This is not what you said at the beginning:

I'm ultimately trying to concatenate keys of the subhashes together where the top-level key matches ignoring case.

That would be

$VAR1 = { 'monkeyhabits' => 'Scratching' }

What are you really trying to do? And why?

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}


Comment on Re: Tied multidimensional hashes
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2014-07-30 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls