For example:
Output: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 (<DATA>) { 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 (<DATA>) { 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 resu +lts 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 c +ourse'); 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
Retrieving data is just one line of Perl-code and since the data is saved in the database you can use it again and again without running the cost of parsing the input file(s).
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 DeltronicsIn reply to Re: Finding Minimum Value
by CountZero
in thread Finding Minimum Value
by jimmy88
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |