in reply to 2 txt files comparison and sorting

Instead of arrays, consider reading your two files into a hash-of-hashes data structure. The ID would be the key of the upper hash, and name and grades would be the keys of the lower hashes. Then you can loop through the structure for your print out. Refer to perldsc. Use exists to check if a student has a grade, etc.