How to add a new hash to a hash array?

If I wanted to add a new hash to all arrays in mother_hasha 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' 
        } 
    ],
    #...
);
+4
source share
3 answers

If you have a hash of hash arrays 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 repeats the values %mother_hash(which in this case are ref arrays) and sets $_for each iteration. Then, at each iteration, we return a link to the new hash %new_hashto the end of this array.

+2
source

mother_hash - hash of hash arrays.

.

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

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

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

"" //, -/,

 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}));

.., , , .

+1

, - , . :

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

, %daughter_hash3.

:

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

$daughter_hash_key - , %mother_hash %daughter_hash - .

$daughter_hash_key:

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

, , Data::Dumper %mother_hash , , .

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

. perldoc Data::Dumper.

Data::Dumperis the standard module that comes with Perl. The list of standard modules, see. In perldoc perlmodlib.

+1
source

Source: https://habr.com/ru/post/1684023/


All Articles