On May 20, 2009, at 4:50 PM, pa...@compugenic.com 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

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.

Any suggestions would be highly appreciated.

The problem here is that you are looping on the values of the key called "count" and the values for such a key is just a single reference. That's it. That reference only happens to be a reference on an array that contains some strings.

So you have to, either loop again on that reference, or just loop in a dereferenced reference:

You can give it a try like this on the latter:

for my $client (@clients) {
 for my $i(@{ $client->{count} } ) {
  print $i, "\n";
 }
}

David.
http://twitter.com/damog

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to