in reply to Passing an array of hashes to a subroutine
Three problems.
First, $d doesn't contain what you think it does, whatever that is.
foreach my $d (@{$array_of_hashes}) { print $d[0]{'one1'}; }
It contains the element at each index of the array (in turn). In other words, it contains a reference to a hash. Fix:
foreach my $h (@{$array_of_hashes}) { print $h->{'one1'}; }
Secondly, you reference the array @array_of_hashes even though no such array exists.
print $array_of_hashes[0]{'one1'}; print $array_of_hashes[0]{'one2'}; print $array_of_hashes[1]{'two1'}; print $array_of_hashes[1]{'two2'};
Had you used use strict as you should, you would have gotten an error. Fix:
print $array_of_hashes->[0]{'one1'}; print $array_of_hashes->[0]{'one2'}; print $array_of_hashes->[1]{'two1'}; print $array_of_hashes->[1]{'two2'};
See References Quick Reference.
Finally, your array contains two references to the same hash.
my %hash; my @array; ... push @array, \%hash; ... push @array, \%hash;
Fix:
my @array; { my %hash; ... push @array, \%hash; } { my %hash; ... push @array, \%hash; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Passing an array of hashes to a subroutine
by blazar (Canon) on May 21, 2007 at 22:01 UTC | |
by ikegami (Patriarch) on May 22, 2007 at 05:29 UTC | |
by blazar (Canon) on May 22, 2007 at 10:19 UTC |