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

In reply to Re: 2 txt files comparison and sorting by CountZero
in thread 2 txt files comparison and sorting by keltan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.