Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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.


Comment on Re: unknown number of dimensions of a multidimensional hash
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://535574]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2014-10-01 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (3 votes), past polls