On Fri, Jun 01, 2001 at 03:29:43PM -0700, Chuck Ivy wrote:
> First post, quick question:
>
> I've got an array of hashes that I'm defining the most basic way I can...
>
> my $gSeasonID;
> my @season_list = '';
> while (@fields = $sth->fetchrow_array) {
> $gSeasonID = $fields[0];
> $season_list[$gSeasonID]{number} = $fields[1];
> $season_list[$gSeasonID]{title} = $fields[2];
> $season_list[$gSeasonID]{active} = $fields[3];
> }
>
> where @fields is coming from a DBI query.
>
> Essentially I have
>
> $season_list[1]{number} = 1;
> $season_list[1]{title} = 'Season One';
> $season_list[1]{active} = 0;
> $season_list[2]{number} = 1;
> $season_list[2]{title} = 'Season Two';
> $season_list[2]{active} = 1;
>
> That's fine, and it seems to be writing correctly. But I'd like to loop
> on my array index later to get back, say {title} from each season.
>
> Is there a clean way to do this with foreach or while? What I'd be
> looking for would be $season_list[$loop_season]{name}, if I were using a
> for loop with $loop_season as my index. Is there a way to do this
> with $_ or something and foreach so that I don't have to know the size
> of my array?
Each element in @season_list is just a reference to a hash, so you can
loop through it with foreach using code like this:
foreach my $sl (@season_list) {
print "$sl->{title}\n";
}