my $cvterm_id; # this scalar is going to be a hashref while (my $hashref = $sth->fetchrow_hashref) { $cvterm_id->{$hashref->{termname}} = $hashref->{cvterm_id}; }