On Wed May 20 2009 @ 1:50, [email protected] wrote:
> I have the following data structure defined:
>
> my @clients = (
> {
> name => 'joe',
> count => [ qw( one two three ) ]
> }
> );
>
> Then I try running the following routine:
>
> for my $client (@clients) {
> for my $i ($client->{count}) {
> print "$i\n";
> }
> }
>
> I expect it to print the following:
> one
> two
> three
>
> But instead, it prints out the following:
> ARRAY(0x8b4b880)
>
> If I change it to:
> for my $i (@$client->{count}) {
> print "$i\n";
> }
>
> I get the error:
> Not an ARRAY reference at ./test_data_structure.pl line 12.
>
>
> My question is what am I doing wrong? How to I loop over each element
> in my anonymous array to process it? I've read the data structure
> tutorials and tried different things, but just can't seem to get this to
> work.
The problem is that you can only use @$array_ref if array_ref is a simple
scalar. What you want to put there is itself a reference - client->{count}
so you need brackets to refer to the entire array.
This should do what you want:
for my $client (@clients) {
for my $i (@{$client->{count}}) {
print "$i\n";
}
}
You might also take a look at this: http://www.perlmonks.org/?node_id=69927
I hope this helps, T
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/