in reply to Correct Loop Structure

It's not the loop structure, it's the variable. You're setting $i to every element of @newips, but printing $_. Either change the foreach to use $_, like so:

foreach (@newips) {
Or change the print:
print OUTFILE $i if ...

HTH