I seem to have an array of hashes, I need to look up a each key => values in the hashes. I am not able to loop through each hash and find the key => values more than once. I know this is a very simple beginner problem and I have been reading stuff on Perl for days including: http://www.perlmonks.org/?node=References%20Quick%20Reference http://perldoc.perl.org/Data/Dumper.html and the all Perl books I have. What I am posting here is some code I whipped up that replicates the problem I am getting. I recreated the PR variable with the output of Data::Dumper in the real program, thats why I said 'seem to have an array of hashes' because I am not sure I am reading the output correctly but it matches what I get from this little bit of code. I created some variables that would be present from program. 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.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; #test program #set up some test data: my $form; $form->{"id"} = '12594'; $form->{"cleared"} = '1'; print qq| ID: $form->{"id"} $form->{"cleared"}\n|; #set up what we seem to get from the web program #an array called $form->{PR} #another unnamed array called $VAR1 by data dumper? #var1 contains a list of hashes @{ $form->{PR} } = ( { 'source' => '8', 'name' => [ 'Telecom' ], 'description' => 'Telecom', 'entry_id' => 1583, 'transdate' => '08-10-2013', 'amount' => '-80', 'fx_transaction' => 0, 'id' => 10368, 'cleared' => 0 }, { 'source' => '', 'name' => [ 'iris' ], 'description' => 'iris', 'entry_id' => 1668, 'transdate' => '02-01-2014', 'amount' => '100', 'fx_transaction' => 0, 'id' => 12585, 'cleared' => 0 }, { 'source' => '12', 'name' => [ 'critter' ], 'description' => 'critter', 'entry_id' => 1670, 'transdate' => '02-01-2014', 'amount' => '12', 'fx_transaction' => 0, 'id' => 12592, 'cleared' => 0 }, { 'source' => '12', 'name' => [ 'critter' ], 'description' => 'critter', 'entry_id' => 1672, 'transdate' => '02-01-2014', 'amount' => '12', 'fx_transaction' => 0, 'id' => 12594, 'cleared' => 0 } ); #see if it loks right? print "PR:\n", Dumper $form->{PR}; #attempt look up a value: #my $elem ; foreach my $key ( @{ $form->{PR} } ) { if ( UNIVERSAL::isa( $key,'HASH') ) { #my %elem = %$key ; my %elem = %{$key} ; print "HASH ELEM:\n", Dumper %elem; foreach my $var ( keys %elem ) { if ($var->{id} == $form->{"id"}){ print qq|\t\telem->key $var->{id} eq $form->{"id"}\n|; print qq|\t\t cleared? $var->{cleared} $form->{"cleared"}\n| +; if ( $var->{cleared} != $form->{"cleared"} ){ $var->{cleared} = $form->{"cleared"}; print qq|\t\t FIXED $var->{cleared} $form->{"cleared"}\n|; } }#end if else {print qq|var id: $var->{id} ne form $form->{"id"}\n|; +} }#end loop array } if ( UNIVERSAL::isa( $key,'ARRAY') ) { my @elem = @$key ; #my @elem = $key ; #go down the columns print "ARRAY ELEM:\n", Dumper @elem; foreach my $var ( @elem ) { if ($var->{id} == $form->{"id"}){ print qq|\t\telem->key $var->{id} eq $form->{"id"}\n|; print qq|\t\t cleared? $var->{cleared} $form->{"cleared"}\n| +; if ( $var->{cleared} != $form->{"cleared"} ){ $var->{cleared} = $form->{"cleared"}; print qq|\t\t FIXED $var->{cleared} $form->{"cleared"}\n|; } }#end if else {print qq|var id: $var->{id} ne form $form->{"id"}\n|; +} }#end loop array } }#end for each elem print "done\n";
Thanks in advance

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