Your script gets much much simpler by putting your data in a database and then using standard SQL to retrieve the data. When working with data and manipulating the data in various ways, always think "database"!

For example:

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 ****************
Output:
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 Deltronics

In reply to Re: Finding Minimum Value by CountZero
in thread Finding Minimum Value by jimmy88

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.