in reply to foreach in array

In addition to the advice you have already received, a quick way to verify that the number of elements in an array is what you expect it to be is to use scalar:
print scalar @data, "\n";