References - Solution to Sample Program

#!/usr/bin/perl
use strict;
use warnings;

=begin COMMENTS

Program: makeseed.pl
by J. David Eisenberg

PURPOSE:
========
This program assists in setting up amateur wrestling tournaments.
It takes a file of competitors, organized by school, and produces
output files organized by weight class. These files are used to
produce printed bracket sheets for the tournament.

INPUT:
======
The input file, named all_teams.txt, contains all team rosters.
Each roster starts with the team name on a single line by itself,
followed by competitor weights and names (one line each).

Weights and names are separated by whitespace. If a wrestler is
seeded, the seed number is separated from the name by a slash, with
optional whitespace on either side of the slash.  Example:

    Peoria High School
       103 Joe Schemegeggie
       112 John Doe/2
       119 Juan Fulano

PROCESSING:
===========
The program builds a hash of hashes. The primary hash has
weight class as its key; the value is a secondary hash whose
key is a school name, and whose value is a person's name (and seed).

OUTPUT:
=======
One file for each weight class. File name has format seednnn.txt,
where "nnn" is the weight class.  The first line in the file
duplicates the weight class number, and is followed by all the
competitors at that weight (one line each), in the format

    name, school, seed

Thus, John Doe (above) would appear in file seed112.txt on a
line reading:

    John Doe, Peoria High School, 2


=end COMMENTS

=cut


my %weight_hash = ( );
my $data;
my $weight;
my $school;
my $name;
my $seed;

open INFILE, "all.txt" or die("Cannot find input file.");

#
#   Input phase
#
while ($data = <INFILE>)
{
    chomp $data;
    
    #   The next line gets rid of newlines from DOS files when
    #   reading on a UNIX system.
    $data = trim($data);
    
    if ($data =~ m/^\s*[A-Za-z]/)   # school name
    {
        # print "School: $data\n";
        $school = $data;
    }
    elsif ($data =~ m/^\s*(\d+)\s+(.*)/) # weight/name/seed
    {
        $weight = trim($1);
        $name = trim($2);   # this may include seed info
    
        # print "Weight: $weight name: $name school: $school\n";
            
        $weight_hash{ $weight }->{$school} = $name;
    }
    else
    {
        # print "Ignoring $data\n";
    }
}
close INFILE;

#
#   Output phase
#

foreach $weight (sort {$a <=> $b} keys %weight_hash)
{
    open OUTFILE, ">seed$weight.txt" or die("Cannot open seed$weight.txt");
    print "Creating seed$weight.txt\n";

    print OUTFILE $weight, "\n";

    foreach $school (sort keys %{$weight_hash{$weight}})
    {
        $name = $weight_hash{$weight}->{$school};
    
        #   split the name on slashes - if no seed,
        #   then $seed will be undefined
        #
        ($name, $seed) = split( /\//, $name );
    
        $name = trim($name);
        print OUTFILE "$name, $school";
        if ($seed)
        {
            $seed = trim($seed);
            print OUTFILE ", $seed";
        }
        print OUTFILE "\n";
    }
    
    close OUTFILE;
}


sub trim
{
    my ($str) = @_;
    $str =~ s/^\s+//;
    $str =~ s/\s+$//;
    return $str;
}