Hi, I am currently working on my first perl program. Which would make this my first post here at perlmonks so take it easy on me :P

I am stuck on attempting to make an array of hashes, where each hash has both arrays(of scalars) and scalars within it. There error I am currently getting is:

Can't coerce array into hash at calcoverlap.pl line 222.

1 is {

here is where I set up the hash:
%imolecule = ( 'maxval' => [@maxval], 'minval' => [@minval], 'range' => [@mp], 'label' => $molcount, 'peers' => '0', ); push @allmolecules, %imolecule;
this part gets looped for as many times as there are molecules in a file. I reuse the '%imolecule' hash each loop, clearing it and refilling it with data. At the end of the loop I try to print out the hash array to see if it worked at all:
for $i ( 1 .. $#allmolecules ) { print "$i is { "; for $field ( keys %{ $allmolecules[$i] } ) { #line 222 if (ref($allmolecules[$i]{$field}) eq "ARRAY") { print "$field=@{$allmolecules[$i]{$field}}"; } else { print "$field=$allmolecules[$i]{$field}"; } } print "}\n"; }
The above error indicates that the problem lies with this line of code:
for $field ( keys %{ $allmolecules[$i] } ) {
which would mean the allmolecules[i] isn't a hash.

Does anyone know what my mistake would be?

Also, since I reuse the imolecule hash each loop, I can't push it as a reference, but is there a better way to be doing it?

Appreciate any help :)

-tom

Update: Wow, thanks for the quick responses. I didn't realize that using {} would allocate new memory for my hash each round, allowing me to use the same hash name each loop. Dumper was very useful for me as well. I will definitely read that suggested material so my next problem won't be so easy for you guys to solve ;). Thanks again :)


In reply to 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.