You've already received some good advice about the question you originally asked, but here are a few more pointers:
- Inside your foreach loop you are creating a new database handle. This is very inefficient and should be avoided. You want to create a database handle once for an average script and reuse it, rather than paying the connection overhead multiple times. Also, some databases have rather low limits on how many connections can be opened concurrently.
- You received a recommendation about having SQL do the work -- I would definitely take that advice. In cases where you need to step through a list of values, reusing those values in a query, you'll want to read the DBI documentation until you understand how to prepare a statement handle and use the '?' placeholders (to avoid the overhead of recompiling your SQL statement a large number of times).
- I suspect you are still not writing 'use strict;' and 'use warnings;' at the top of your scripts. Whenever you post a code sample, you'll do well to show that you are doing this, if only to avoid people like me saying, "You really should 'use strict' and 'use warnings'! :)
Good luck!
No good deed goes unpunished. -- (attributed to) Oscar Wilde