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:
And here's the ouput:#!/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;
$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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |