Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: unknown number of dimensions of a multidimensional hash

by duff (Vicar)
on Mar 10, 2006 at 00:47 UTC ( #535574=note: print w/replies, xml ) Need Help??

in reply to unknown number of dimensions of a multidimensional hash

Another thing you can do is use concatenated keys rather than a multidimensional data structure. (I don't know how critical the hierarchical nature is to your problem solution) That is, instead of:

$data {$a}{$b} $data {$a}{$b}{$c} $data {$d}{$e}
you would use
$data{join "\0", $a, $b} $data{join "\0", $a, $b, $c} $data{join "\0", $d, $e}
Then to access the keys you'd use your standard keys operator:
for (keys %data) { # you could do something with $data{$_} directly, # or if you need access to the individual keys ... my @keys = split /\0/; # ... }
The main caveat is that your "key separator" should be something that doesn't appear in your keys. I chose "\0" in this example assuming that your keys aren't likely to contain NUL bytes.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2017-11-23 13:26 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (335 votes). Check out past polls.