in reply to 2 txt files comparison and sorting

Or think of these two text files as database tables and you can use the power of SQL and DBI to solve your assignment:
use Modern::Perl qw /2014/; use DBI; use Data::Dump qw /dump/; my $dbh = DBI->connect("dbi:CSV:f_dir=."); $dbh->{csv_allow_whitespace} = 1; my $sql = 'SELECT names.firstname, names.name, names.ID, scores.ID, scores.score + FROM names FULL OUTER JOIN scores ON names.ID = scores.ID'; my $sth = $dbh->prepare($sql); $sth->execute(); while ( my $row = $sth->fetchrow_hashref ) { say dump($row); } $sth->finish();
Output:
{ "names.firstname" => "John", "names.ID" => 34267, "names.name" => "Kowalski", "scores.ID" => undef, "scores.score" => undef, } { "names.firstname" => "Eve", "names.ID" => 11232, "names.name" => "Nowak", "scores.ID" => 11232, "scores.score" => "5.0", } { "names.firstname" => "Theodor", "names.ID" => 3423, "names.name" => "McNack", "scores.ID" => 3423, "scores.score" => 3.5, } { "names.firstname" => undef, "names.ID" => undef, "names.name" => undef, "scores.ID" => 55342, "scores.score" => "2.0", }
The only change I had to make to these text files was to add a first line with the field names.

The SQL for the exam results is SELECT names.firstname, names.name, scores.score FROM names JOIN scores ON names.ID = scores.ID

The list of absent students can be found with this SQL: SELECT names.firstname, names.name FROM names LEFT OUTER JOIN scores ON names.ID = scores.ID WHERE scores.score IS NULL and the unidentified results can be found with SELECT scores.ID, scores.score FROM names RIGHT OUTER JOIN scores ON names.ID = scores.ID WHERE names.name IS NULL

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics