Here's my driving code, test.pl. I can get it to read the second dimension on the subsequent FETCH, but can't figure out how to write the recursive routine. The only recursive way I know is if I can supply the next parameter (eg. $self->FETCH( $dimension ), or implicitly, $node->{ $dimension }, where $dimension would first be 'x' and secondly 'y'). But the hash tie() API only presents one dimension at a time. A neat thing I have learned, so far, is that for storing multidimensional hashes, that the ongoing case is that the value will be an anonymous hash, and the terminus case is that the value is a scalar.

test.pl

#! /users/michwong/perl/bin/perl #! /users/michwong/perl/bin/perl -d:ptkdb use lib qw( . ); use MultiHash; tie %$hash, "MultiHash"; $hash->{ x }{ y } = 10; $hash->{ x }{ y };

Here's my module, MultiHash.pm

MultiHash.pm

package MultiHash; sub TIEHASH { my ($class) = map { ref || $_ } shift; my $level = shift || 0; return bless { level => $level, data => {}, }, $class; } sub FETCH { my $self = shift; my $key = shift; if( UNIVERSAL::isa( $self, 'HASH' )) { print "($self) fetching '$key': $self->{ data }{ $key }\n"; } else { print "($self) fetching '$key': $self->{ data }{ $key }\n"; } return $self->{ data }{ $key }; } sub STORE { my $self = shift; my $key = shift; my $value = shift; RECURSE_CASE: { # ===== MULTIDIMENSIONAL HASH if( UNIVERSAL::isa( $value, 'HASH' )) { if( exists $self->{ data }{ $key } ) { $self->{ data }{ $key } = $value; print "($self) storing '$value' into '$key'\n"; } else { my $node; tie %$node, "MultiHash"; $self->{ data }{ $key } = $node; print "($self) storing '$value' into '$key' with new n +ode\n"; } last RECURSE_CASE; } if( UNIVERSAL::isa( $value, 'MultiHash' )) { if( exists $self->{ data }{ $key } ) { $self->{ data }{ $key } = $value; print "($self) storing '$value' into '$key'\n"; } else { my $node; tie %$node, "MultiHash"; $self->{ data }{ $key } = $node; print "($self) storing '$value' into '$key' with new n +ode\n"; } last RECURSE_CASE; } if( UNIVERSAL::isa( $value, 'SCALAR' )) { $self->{ data }; last RECURSE_CASE; } } return $value; } sub DELETE { my $self = shift; my $key = shift } sub FIRSTKEY { my $self = shift; my $temp = keys %{ $self->{ data }}; return scalar each %{ $self->{ data }}; } sub NEXTKEY { my $self = shift; return scalar each %{ $self->{ data }}; } 1;

In reply to My code (I can't figure out how to implicitly recurse) by mikezone
in thread How to tie multilevel (multidimensional) hash? by mikezone

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.