[HRPM] Perl beginner looking for a little help

Jeff Self jocknerd at home.com
Mon May 21 16:00:11 CDT 2001


I wrote a program in C a couple of years ago that read in football scores
and computed rankings. As I am trying to learn perl now, I figured this
would be a good program to work with. So far in my endeavors, my perl
program can read in the input file and print out the scores for each game.
What I need to do now is have each game stored in a hash I presume. What I
am trying to figure out is how to create the hash that will hold the data
for each game and the hash storing data for each team. If anyone's
interested in looking at the data, you can get the file from
http://www.mratings.com/data/nfl2000.txt.  Here's what I've got right now:
********************************************************************
# Football Rankings Program

# Read in the scores
$a = "nfl2000.txt";
open(SCORES,$a) || die "cannot open $a for reading: $!\n";

while (<SCORES>) {	#read a line from file $a into $_
	s/^\s+//;		#discard leading whitespace
	s/\s+$//;		#discard trailing whitespace
	s/\s+/ /g;		#collapse internal whitespace
	($month,$day,$year,$vteam,$vscore,$hteam,$hscore) = $_ =~
	/^(\d+)\D(\d+)\D(\d+)(\D+)(\d+)(\D+)(\d+)(.*)$/;

	# Adjust their scores
	$adj_vscore = &get_adjusted_score($vscore);
	$adj_hscore = &get_adjusted_score($hscore);

	# Get expected ratio
	#$expected_ratio = &get_expected_ratio($vrate,$hrate);

	# Get the Game Ratio
	$game_ratio = &get_game_ratio($adj_vscore, $adj_hscore);

	printf("%s\t%d\t%s\t%d\t%.4f\n",$vteam,$vscore,$hteam,$hscore,$game_ratio);
}

##################################################
# Subroutines
##################################################

##################################################
# Adjust Score
# This is implemented to prevent teams from
# running up the score. There is a maximum
# adjusted score of 50.0
##################################################
sub get_adjusted_score
{
	$adjden = 250.0;
	($adj_score) = @_;
	$result = $adj_score - ($adj_score ** 2) / $adjden;
	return ($result);
}

##################################################
# Game Ratio
# This is used to provide a way to measure the
# outcome of a game.
##################################################
sub get_game_ratio
{
	($adjvscore,$adjhscore) = @_;
	$result = (($adjvscore/6) ** 2 + 1.0)/(($adjvscore/6) ** 2 +
($adjhscore/6) ** 2 + 2.0);
	if ($adjvscore > $adjhscore)
	{
		$result += 1.0;
	}
	elsif ($adjvscore == $adjhscore)
	{
		$result += 0.5;
	}
	return ($result * 0.5);
}

**********************************************************************

Here's the structs from my C program:

/* This is a Game struct */
struct Game {
	struct Team *vteam, *hteam;
	int vscore, hscore;
	double ratio;
	struct Game *next;
};

/* This is a Team struct */
struct Team {
	char *name;
	int won,lost,tied,pf,pa;
	double rating;
	struct Team *next;
};

Thanks,
Jeff Self

-- 
Hard Work Often Pays Off After Time, but Laziness Always Pays Off Now.




More information about the Norfolk-pm mailing list