Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I seem to be having a problem STOREing multidimensional values in a TIEd hash. I'm now going round in circles because even my minimal test case (which is typically all I need to produce to fix my own problem) isn't doing what I expect.

I've reviewed How to tie multilevel (multidimensional) hash? and the Tie::Hash documentation, and I've frankly run out of brain power.
I'm ultimately trying to concatenate keys of the subhashes together where the top-level key matches ignoring case. However, the code below doesn't even warn twice (it warns once with an empty hashref), suggesting it's broken in a stupid way I can't spot, or I'm not grokking Ties.

use warnings; use strict; use Data::Dumper; use Tie::Hash; package FlatHash; use Data::Dumper; our @ISA = qw(Tie::ExtraHash); sub STORE { my $me = $_[0]; my $key = $_[1]; my $value = $_[2]; $key = lc $key; $_[0][0]{$key} = $value; if(ref $value eq "HASH") { warn Dumper($value); } else { print "This isn't a hash\n"; } } package main; tie my %flat, "FlatHash"; $flat{Monkey}{habits} = "Eating"; $flat{monkey}{habits} = "Scratching"; warn Dumper(\%flat);
Ultimately I'd like the final datastructure to be something like:
$VAR1 = { 'monkey' => { 'habits' => ['Eating', 'Scratching'] } };
Any hints gratefully received.

davis
Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.

In reply to Tied multidimensional hashes by davis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (7)
    As of 2015-07-03 21:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (56 votes), past polls