First ugly thought goes like this:
my @season_list = '';
while (my @fields = $sth->fetchrow_array) {
for (my $i = 1; $i < @fields; $i++) {
$season_list[$fields[0]]{('number', 'title', 'active')[$i]} = $fields[$i];
}
Second thought is:
Why not use $sth->fetchrow_hashref? The only reason I can think of
would be a case where your select looked like this
select a.id, b.name, a.name from tab1 a, tab2 b where a.id = b.id;
in this case Informix (and many other databases) returns (id, name,
name) from the $sth->{NAME} call. This can be corrected by writing you
sql like this (please adjust for your flavor of sql):
select a.id, b.name as b_name, a.name as a_name
from tab1 a, tab2 b
where a.id = b.id;
On 01 Jun 2001 15:29:43 -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?
--
Today is Boomtime, the 6th day of Confusion in the YOLD 3167
Fnord.