keltan has asked for the wisdom of the Perl Monks concerning the following question:

Hi i've got 2 txt files. 1.
John, Kowalski, 34267 Eve, Nowak, 11232 Theodor, McNack, 3423
2.
11232, 5.0 55342, 2.0 3423, 3.5
What do i need to do is:
Exam results: 1. Eve Nowak 5.0 2. Theodor McNack 3.5 absent: 1. John Kowalski unidentified: 1. 55342
As you can see in first text file i've got students with their numbers and in the second one i've got grades and numbers. What i need to do is sth like this:
#!/usr/bin/perl -w $students = $ARGV[0]; $results = $ARGV[1]; open my $fh, '<', $students; while(<$fh>) { @def = split /,/, $_; chomp(@def1); print "@def"; } open my $fg, '<', $results; while(<$fg>){ @def1=split /,/,$_; chomp(@def1); print "@def1"; } close($fg); close($fh);
I've written so far this code but i really don't know how to solve this task.

Replies are listed 'Best First'.
Re: 2 txt files comparison and sorting
by CountZero (Bishop) on Apr 20, 2015 at 19:57 UTC
    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
Re: 2 txt files comparison and sorting
by NetWallah (Canon) on Apr 20, 2015 at 18:37 UTC
    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

Re: 2 txt files comparison and sorting
by toolic (Bishop) on Apr 20, 2015 at 18:38 UTC
    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.