in reply to How to write a sequence of files?

All the records appear in the file, it ‘s just they all end up in the same file.

And what is the name of that file? Is the four-digit "company code" present or absent in the file name? You have not shown us how values get assigned to your "$passCompCode" variable, and the problem may just be that this variable's value is never changing.

The code seems like it would do the right thing if given distinct values for that variable. One thing I would change, though, in the "get_Comp_Code" sub:

if ( $companyRef > $#companyArray or $companyRef < 0 ) {
The point is that the condition depends on the size of the array, not some "magic number" like "52".