bluesplay106 has asked for the wisdom of the Perl Monks concerning the following question:

I've been looking at the PerlMonk forum for answers, but so far nothing has worked. I realized that referencing the innerhash makes reusing the innerhash impossible. I want to fill the innerhash, store it for a key in hash and then reset innerhash to take in new information and be stored in for a new key in hash. Are there possible ways of doing that?
my %hash = (); my %innerhash = (); # Fills hashes with stuff $hash{$somekey} = \%innerhash; # Trying to loop through each key in inner hash foreach my $key (keys %hash) { while(my ($key1, $value1) = each (%{$hash{$key}}) ) { print "$key1 $value1\n"; } }

Replies are listed 'Best First'.
Re: Hash in a Hash Issue
by toolic (Bishop) on Jul 23, 2013 at 18:53 UTC
Re: Hash in a Hash Issue
by NetWallah (Canon) on Jul 24, 2013 at 06:05 UTC
    My interpration of your question is somewhat different from the responses posted thus far - it looks like you want to modify %innerhash, after saving a "reference" to it, and you fear (correctly) that the reference will also change.

    The answer is that what you save should be a COPY, not a reference.

    If the %innerhash is only one level deep (i.e. it is shallow), you can save a copy like this:

    $hash {somekey} = {%innerhash}; # Shallow copy
    Now, you are free to modify %innerhash, and $hash{somekey} will maintain it's original value.

    IF %innerhash is more than one level deep, you will need to do a "deep copy" - search for that at this site if you need it.

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: Hash in a Hash Issue
by nevdka (Pilgrim) on Jul 23, 2013 at 23:49 UTC

    It looks like you want to use an anonymous hash:

    use strict; use warnings; my %hash = ( 'key 1' => { 'innerhash1 key 1' => 'some data', 'innerhash1 key 2' => 'some more data', }, 'key 2' => { 'innerhash2 key 1' => 'even more data', 'innerhash2 key 2' => 'a bit more data', }, ); foreach my $key (keys %hash) { foreach my $inner_key ( keys %{$hash{$key}} ) { print "$inner_key $hash{$key}->{$inner_key}\n"; } }

    $hash{'key 1'} contains a reference to the inner hash, but the inner hash itself is no longer named. You could rework the rest of your code to put data directly into there, and not have to worry about overwriting the data you've put in another inner hash.

    If you want to seek more wisdom on your own, search for 'hash of hashes' or HoH if you're on a Perl site (like this one).

Re: Hash in a Hash Issue
by 2teez (Vicar) on Jul 23, 2013 at 23:07 UTC

    Hi bluesplay106,
    I've been looking at the PerlMonk forum for answers, but so far nothing has worked..

    Really? Looking at perldsc should help I suppose?
    I think, it would also not be out of place to fully follow the counsel in How do I post a question effectively?

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me