Hi Monks,

I haven't dared tread this hallowed ground before now, but looking at some of the other questions, it looks like apprentices like me are welcome. In any case, I have a problem understanding referencing and dereferencing (well, i think that's my problem). Here is my code:

#!/ms/dist/perl5/bin/perl5.8 # Standard module imports use Data::Dumper; use strict; use warnings; my $hashArrayRef; $hashArrayRef = [{ 'dateOfBirh' => '22 March 1971', 'firstName' => 'Ronnie', 'lastName' => 'Smith' }, { 'timeNow' => '14 April 1972', 'firstName' => 'Claudia', 'lastName' => 'Winkleman' }]; print Dumper(\$hashArrayRef); print "\n"; my @deRefHashArray = @$hashArrayRef; print Dumper(@deRefHashArray); print "\n"; print scalar @deRefHashArray; print "\n"; my %resultHash; my $countResult = $#deRefHashArray; print $countResult; print "\n"; for (my $counter = 0; $counter < $countResult; $counter++) { %resultHash = {$deRefHashArray[$counter]}; while ( my ($key, $value) = each(%resultHash) ) { print "$key => $value\n"; } } 1;

And here's the ouput:

$VAR1 = \[ { 'firstName' => 'Ronnie', 'dateOfBirh' => '22 March 1971', 'lastName' => 'Smith' }, { 'firstName' => 'Claudia', 'timeNow' => '14 April 1972', 'lastName' => 'Winkleman' } ]; $VAR1 = { 'firstName' => 'Ronnie', 'dateOfBirh' => '22 March 1971', 'lastName' => 'Smith' }; $VAR2 = { 'firstName' => 'Claudia', 'timeNow' => '14 April 1972', 'lastName' => 'Winkleman' }; 2 1 Odd number of elements in anonymous hash at arrayTest_monk.pl line 38. Reference found where even-sized list expected at arrayTest_monk.pl li +ne 38. Use of uninitialized value in concatenation (.) or string at arrayTest +_monk.pl line 40. HASH(0x82007a4) =>

Essentially, I have an array of hashes that is referenced. I want to iterate over the array, and then access the hash inside each of the elements - even in my example just to iterate over them too and print out the results. The problem comes in accessing the hashes - I can't grasp what I need to do to persuade my program that the element in the array is a hash, and therefore let me access it...

I know this post is long, but hopefully there's enough info to see what I'm strugggling with.

Many thanks.

In reply to arrays, hashes, dereferencing confusion - need help by Klunk

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.