Mr Keystrokes
Mr Keystrokes

Reputation: 81

How do I append a new hash to an array of hashes?

If I wanted to add a new hash to all the arrays in the mother_hash using a loop, what would be the syntax?

My hash:

my %mother_hash = (
    'daughter_hash1' => [ 
        { 
          'e' => '-4.3', 
          'seq' => 'AGGCACC', 
          'end' => '97', 
          'start' => '81' 
        } 
    ],
    'daughter_hash2' => [ 
        { 
          'e' => '-4.4', 
          'seq' => 'CAGT', 
          'end' => '17', 
          'start' => '6' 
        }, 
        { 
          'e' => '-4.1', 
          'seq' => 'GTT', 
          'end' => '51', 
          'start' => '26' 
        }, 
        { 
          'e' => '-4.1', 
          'seq' => 'TTG', 
          'end' => '53', 
          'start' => '28' 
        } 
    ],
    #...
);

Upvotes: 4

Views: 2772

Answers (3)

Håkon Hægland
Håkon Hægland

Reputation: 40778

If you have a hash of arrays of hashes and want to add a new hash to the end of each of the arrays, you can do:

push @{ $_ }, \%new_hash for (values %mother_hash);

This loop iterates over the values of %mother_hash (which are array refs in this case) and setting $_ for each iteration. Then in each iteration, we push the reference to the new hash %new_hash to the end of that array.

Upvotes: 2

shawnhcorey
shawnhcorey

Reputation: 3601

First I would point out the daughter hashes aren't hashes but arrays of anonymous hashes. To add another daughter hash:

$mother_hash{daughter_hash3} = [ { %daughter_hash3 } ];

This creates an anonymous array that contains an anonymous hash with the contents of %daughter_hash3.

For a loop:

$mother_hash{$daughter_hash_key} = [ { %daughter_hash } ];

where $daughter_hash_key is a string contain the key for the %mother_hash and %daughter_hash is the hash to add.

To add another hash to a daughter array with key $daughter_hash_key:

push @{ $mother_hash{$daughter_hash_key} }, { %daughter_hash };

I know ti's complicated but I suggest you use Data::Dumper to dump the contents of %mother_hash each time thru the loop to see if it grows correctly.

use Data::Dumper;
print Dumper \%mother_hash;

See perldoc Data::Dumper for details..

Data::Dumper is a standard module that comes with Perl. For a list of standard modules, see perldoc perlmodlib.

Upvotes: 2

Edwin Buck
Edwin Buck

Reputation: 70959

mother_hash is a hash of arrays of hashes.

To add another top-level array of hashes.

%mother_hash{$key} = [ { stuff }, { stuff } ];

To add another entry to an existing array

push @{%mother_hash{'key'}} { stuff };

To add another entry to the hash in the embedded array

%{@{%mother_hash{'top_key'}}[3]}{'new_inner_key'} = value;

When confused and attempting to match up the "types" of hash / array / scalar containing a hash reference / array reference, you can use the following technique

 use Data::Dumper;
 $Data::Dumper::Terse = 1;
 printf("mother_hash reference = %s\n", Dumper(\%mother_hash));
 printf("mother_hash of key 'top_key' = %s\n", Dumper(%mother_hash{top_key}));

and so on to find your way through a large data structure and validate that you are narrowing down to the region you want to access or alter.

Upvotes: 1

Related Questions