Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Tied multidimensional hashes

by shmem (Chancellor)
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"; ...


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?


_($_=" "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}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://578116]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-02-21 12:06 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (279 votes). Check out past polls.