OK, to each foreach loop doing one line at a time with $dbh->do("INSERT INTO test (overlay,billa,billb) VALUES ('$overlay','$billa','$billb')");
is duplicating the records in the database, because of the nesting on the foreach loops.
Comment on Re^2: Inserting Multiple Records in DB.