- Add code tags: Writeup Formatting Tips
- I see no need to read position.txt into an array. You can use a while loop instead of the foreach loop. This should easy the memory issue.
- Perhaps use a hash instead of an array for file_data. This may speed things up. (UPDATE: On 2nd thought, probably not.)
Can you show us 10 representative lines from each file?