in reply to 2 txt files comparison and sorting

This smells like homework.

You need a data structure to store, and lookup information.

I suggest using a hash:

my %student ; # content should be <id> => <name>
After you read and split the first file, populate the hash:
$student{$def[2]} = {FIRSTNAME=>$def[0], LASTNAME=>$def[1]};
When you read the grade, lookup and assign into the same structure:
$student{$def1[0]}{GRADE} = $def[1];
(untested)

        "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams