in reply to Looping through an array of objects
then looping would work like this:print $people[0][0]->name;
Or you could do it like this:foreach ( @{$people[0]} ) { print $_->name; }
The point is that each element in @people is itself an array reference, and needs to be treated as such. In the first example, in order to dereference an element of @people as an array, you need the curly braces between the "@" and the expression that represents the array ref.foreach $row ( @people ) { foreach $cell ( @$row ) { print $cell->name; } }
In the second example, doing the dereferencing in two stages makes the syntax less messy. At each iteration of the outer loop, $row is set to the array ref for the inner dimension of @people, and is easier to dereference.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Looping through an array of objects
by dalziel (Initiate) on Jan 19, 2004 at 02:44 UTC |