mikezone has asked for the wisdom of the Perl Monks concerning the following question:
Hello wise monks,
I'd like to have some syntactical sugar to wrap a datastructure that I have. What I'd like to do is say:
and have custom behaviour, based on the values of 'x', 'y', and 'z', and the dimension specified. In other words, if FETCH and STORE took multiple arguments for keys (along the lines of the following:)tie %$hash, "MultiHash"; $hash->{ x }{ y }{ z } = 10;
then I'd just write my code in the MultiHash::STORE and ::FETCH methods and be very happy. But these methods don't work that way.sub STORE { my ($tied_hash, $value, @keys) = @_; my ($x, $y, $z) = @keys; # to stay consistent with above example if( $x == 'foo' ) { ... } ... } sub FETCH { my ($tied_hash, @keys) = @_; my ($x, $y, $z) = @keys; # to stay consistent with above example if( $x == 'foo' ) { ... } ... }
So far, I've tried recursively nesting tied hashes (but that didn't work; the recursive FETCH call apparently received no argument for $key). I've looked at Tie::MLDBM and Tie::RefHash for possible insight but I didn't learn much, and both searching Google and the PerlMonks archive hasn't helped me.
How can I get around this problem?
Thanks in advance,
- m.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: How to tie multilevel (multidimensional) hash?
by merlyn (Sage) on Feb 12, 2003 at 02:35 UTC | |
by mikezone (Novice) on Feb 12, 2003 at 03:46 UTC | |
by mikezone (Novice) on Feb 12, 2003 at 03:36 UTC | |
|
Re: How to tie multilevel (multidimensional) hash?
by perrin (Chancellor) on Feb 11, 2003 at 23:51 UTC | |
by mikezone (Novice) on Feb 12, 2003 at 02:25 UTC | |
by perrin (Chancellor) on Feb 12, 2003 at 03:14 UTC | |
by mikezone (Novice) on Feb 12, 2003 at 03:26 UTC |