use Modern::Perl '2014'; use DBI; my $dbfile = 'c:/data/school.sqlite'; my $dbh = DBI->connect( "dbi:SQLite:dbname=$dbfile", "", "", { RaiseError => 1 } ); my $sth = $dbh->prepare('INSERT INTO students (id, name) VALUES (?, ?)'); while () { chomp; last if /grades.txt/; next unless $_; next if /^\*\*\*/; say $_; my ( $id, $name ) = split /:/; $sth->execute( $id, $name ); } $sth = $dbh->prepare('INSERT INTO results (id, course, score) VALUES (?, ?, ?)'); while () { chomp; next unless $_; next if /^\*\*\*/; my ( $id, $course, $score ) = split /\s+/; $sth->execute( $id, $course, $score ); } my $ary_ref = $dbh->selectall_arrayref( 'SELECT name, sum(score), avg(score), min(score), max(score) FROM results JOIN students WHERE students.id = results.id GROUP BY results.id ORDER BY name' ); printf "%20s %10s %8s %8s %8s\n", qw/Name Total Average Min Max/; for my $line (@$ary_ref) { printf "%20s %10d %8d %8d %8d\n", @$line; } $ary_ref = $dbh->selectall_arrayref( 'SELECT course, avg(score) FROM results GROUP BY course ORDER BY course'); printf "\n%10s %8s\n", qw/Course Average/; for my $line (@$ary_ref) { printf "%10d %8d\n", @$line; } __DATA__ ***students.txt*** 122334:James,Lebron: 222223:Duncan,Tim: 244668:Bryant,Kobe: 355779:Durant,Kevin: ****************** ***grades.txt*** 122334 1 98 222223 1 86 ...(snip)... 244668 5 95 355779 5 94 **************** #### Name Total Average Min Max Bryant,Kobe 466 93 89 95 Duncan,Tim 462 92 86 96 Durant,Kevin 468 93 90 96 James,Lebron 485 97 96 98 Course Average 1 90 2 93 3 95 4 95 5 95