To allocate new memory for the hash, this is similar to what you did for the arrays ie , {%imolecule}.
%imolecule = ( 'maxval' => [@maxval], 'minval' => [@minval], 'range' => [@mp], 'label' => $molcount, 'peers' => '0', ); push @allmolecules, {%imolecule};
Update: So some example code for you... This is an Array of Hash (AoH). You were on the right track realizing that you can't just push a ref to %imolecule. [@maxval] expands @maxval and then puts it into a new hunk memory that is an anonymous array (i.e. makes a "deep" copy). The hash can be handled in the same way. Any way, you can have a "molecule template" that you fill out and then push onto @allmolecules.
#!/usr/bin/perl -w use strict; use Data::Dumper; my @allmolecules; my @maxval = (11,12,13); my @minval = (1,2,3); my @mp = (1,13); my $molcount = 2; my %imolecule = ( 'maxval' => [@maxval], 'minval' => [@minval], 'range' => [@mp], 'label' => $molcount, 'peers' => '0', ); push @allmolecules, {%imolecule}; $molcount = 444; @maxval = (66, 78, 90); %imolecule = ( 'maxval' => [@maxval], 'minval' => [@minval], 'range' => [@mp], 'label' => $molcount, 'peers' => '0', ); push @allmolecules, {%imolecule}; my $n=1; foreach my $href (@allmolecules) { print "Molecule: ", $n++,":\n"; foreach my $key (sort keys %$href) { if (ref($href->{$key}) eq 'ARRAY') { print "$key @{$href->{$key}}\n"; } else { print "$key $href->{$key}\n"; } } print "\n"; } print "Data Dumper output is....\n"; print Dumper \@allmolecules; __END__ output: Molecule: 1: label 2 maxval 11 12 13 minval 1 2 3 peers 0 range 1 13 Molecule: 2: label 444 maxval 66 78 90 minval 1 2 3 peers 0 range 1 13 Data Dumper output is.... $VAR1 = [ { 'maxval' => [ 11, 12, 13 ], 'peers' => '0', 'minval' => [ 1, 2, 3 ], 'range' => [ 1, 13 ], 'label' => 2 }, { 'maxval' => [ 66, 78, 90 ], 'peers' => '0', 'minval' => [ 1, 2, 3 ], 'range' => [ 1, 13 ], 'label' => 444 } ];

In reply to Re: Help creating/printing array of hashes by Marshall
in thread Help creating/printing array of hashes by tomdbs98

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.