in reply to overwriting references in a loop

Before you get too sidetracked, you might want to look at the DBI call selectall_hashref() which returns a hash based on a key field.

As to your references being overwritten, here is an example of the wrong way:
my @foo; my %bar; while ($sth->fetch()) { @bar{'name','age'} = ($name, $age); push (@foo, \%bar); }
This uses the same hash each time, so your array actually contains duplicates. In your example you are declaring it specifically each time, so each %fetch2 is unique. Summary: Declared inside means unique, outside means recycled.

s/\?+/\?/g;   # BTW