At first glance it not seems a behaviour related to flush or autoflush the filehandle and about this (never used) method i read
that seems also not so efficient while itarating over array.
Anyway as the famous motto says 'Bad data ruins your day' check your array using something like
.
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.