When I run this code I would expect HASH ELEM: to print the contents of each anonymous hash in the array not just the first one.

Well, the error message you get is mighty informative :) even more so if you  use diagnostics;

The error

Can't use string ("source") as a HASH ref while "strict refs" in use a +t duck line 77 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref. Uncaught exception from user code: Can't use string ("source") as a HASH ref while "strict refs" +in use at duck line 77.

The error is referring to this code

foreach my $var ( keys %elem ) { if ( $var->{id} == $form->{"id"} ) {
Keys are always strings, they're never references, they're never hash references, so   $var->{id} doesn't make sense as $var is a key it is not %elem

Here is what I would do

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my $form = { PR => [ { cleared => 0, }, [], { deraelc => 1 }, { cleared => 0, }, \'yo', { deraelc => 1 }, ], }; for my $ref ( @{ $form->{PR} } ){ if( UNIVERSAL::isa( $ref,'HASH' ) ){ dd( HASH => $ref ); }elsif( UNIVERSAL::isa( $ref,'ARRAY' ) ){ dd( ARRAY => $ref ); }else{ dd( ELSE => $ref ); } } __END__

Except replace  dd( $ref ); with doHashThing( $ref ); and doArrayThing( $ref ); ... respectively

Then the first thing you do in doHashThing is dd()umper up the hash to see what you can see :) .... I'd even start a new file for each doThing until I'm comfortable with keys/hashes...


In reply to Re: how to loop each anonymous hash in an array of hashes by Anonymous Monk
in thread how to loop each anonymous hash in an array of hashes by aturtle

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.