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
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 %elemforeach my $var ( keys %elem ) { if ( $var->{id} == $form->{"id"} ) {
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |