Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

unknown number of dimensions of a multidimensional hash

by Otogi (Beadle)
on Mar 09, 2006 at 23:10 UTC ( #535551=perlquestion: print w/replies, xml ) Need Help??
Otogi has asked for the wisdom of the Perl Monks concerning the following question:

This most probably has been answered before but can't come up with the right keys to research it. I want to dynamically create a hash with multiple dimensions (number of dimensions dynamically increase) for example:
$data {$a}{$b} $data {$a}{$b}{$c} $data {$d}{$e}
So as the script is running I could add more dimensions to the initial hash using a subroutine called given the initial  $data{$a} . How do I do that? and how would I read the hash to get the keys and values if I don't know the dimension of the hash. Thank you.

Replies are listed 'Best First'.
Re: unknown number of dimensions of a multidimensional hash
by GrandFather (Sage) on Mar 09, 2006 at 23:22 UTC

    The following code is taken from a game engine and manipulates "keys" of the form (\w+\.)+\w+. Each word represents a hash level.

    our %GameState; # The entire game context # looks up a key (passed as the first param) and adds the value # (if provided as second param). Returns a reference to the value # list. sub Entity { my ($key, $value) = @_; return undef if ! defined $key; my $hash = \%GameGlobals::GameState; my @keys = split /\./, $key; map {$_ = lc $_} @keys; while (@keys && defined $hash) { my $key = shift @keys; if (@keys) { $hash->{$key} = {} if ! exists $hash->{$key}; $hash = $hash->{$key}; } else { $hash->{$key} = [] if ! exists $hash->{$key}; push @{$hash->{$key}}, $_[1] if defined $value; return \$hash->{$key}; } } }

    DWIM is Perl's answer to Gödel
Re: unknown number of dimensions of a multidimensional hash
by duff (Vicar) on Mar 10, 2006 at 00:47 UTC

    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.
Re: unknown number of dimensions of a multidimensional hash
by Anonymous Monk on Mar 10, 2006 at 00:47 UTC

    If the hash is not ragged, you simply know its dimensionality. If you somehow lose this information

    sub hashfirst { $_[0]->{($_) = keys %{$_[0]}} } sub shape { my $h = shift; my @s; for (;'HASH' ne ref $h; $h = hashfirst($h)) { push @s, scalar(keys %$h); } @s }

    may help you remember. This feels like an X-Y question.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://535551]
Approved by GrandFather
[davido]: but you stumped me; don't know how to disable proc.
[tye]: I don't know if you can just dismount, but I thought so.
[davido]: yeah, umount -f isn't powerful enough.
[tye]: probably something in the init subsystem that does the mounting that you could disable and reboot.
davido needs to close laptop to board flight home from yapc.
[davido]: I'll look into it in a few hours probably.
[davido]: when i do get to that point I think I'll do it in a vm snapshot just in case. :)
[oiskuu]: tye, you were right: loginuid/sessionid are part of task struct if compiled with AUDITSYSCALL. I have some doubts if you should actually depend on that feature.
[Corion]: oiskuu: Depends on what you want to do with that information
[tye]: I'm not depending on that feature. But I could in this environment. I'm using getlogin(). shrug

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-06-23 20:06 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (555 votes). Check out past polls.