#!/usr/bin/perl

#note that the ../ in front of the file name is because my file is in a 
#different folder. you will probably just have the name of the file
open (FILE, "../soto.tdf");


#I'm going to do this using only the code we've learned. However, there
#are much more elegant solutions we will learn eventually.

#we need to count the number of games, since we're computing averages
$count=0;

#these variables will store the total for each statistic
$total_ab=0;
$total_r=0;
$total_h=0;
$total_2b=0;
$total_3b=0;
$total_hr=0;
$total_rbi=0;
$total_bb=0;
$total_k=0;
$total_sb=0;
$total_cs=0;

while (<FILE>) {
	#remember - variables can't start with a number, so I put the 
	#underscores in front of the 2b and 3b values
	($date, $op, $score,$ab,$r,$h,$_2b,$_3b,$hr,$rbi,$bb,$k,$sb,$cs,$avg,$obp,$slg,$ops) = split(/\t/,$_);

	$total_ab = $total_ab+$ab;
        $total_r = $total_r+$r;
        $total_h = $total_h+$h;
        $total_2b = $total_2b+$_2b;
        $total_3b = $total_3b+$_3b;
        $total_hr = $total_hr+$hr;
        $total_rbi = $total_rbi+$rbi;
        $total_bb = $total_bb+$bb;
        $total_k = $total_k+$k;
        $total_sb = $total_sb+$sb;
        $total_cs = $total_cs+$cs;
	
	$count++;

}

close FILE;

#now, let's print out the totals and averages

#open the file for printing out
open (OUT, ">w2ex2.txt");
print OUT "Season Statistics for Geovany Soto\n";
print OUT "----------------------------------\n\n";

print OUT "Total AB: $total_ab\n";
print OUT "Total R: $total_r\n";
print OUT "Total H: $total_h\n";
print OUT "Total 2B: $total_2b\n";
print OUT "Total 3B: $total_3b\n";
print OUT "Total HR: $total_hr\n";
print OUT "Total RBI: $total_rbi\n";
print OUT "Total BB: $total_bb\n";
print OUT "Total K: $total_k\n";
print OUT "Total SB: $total_sb\n";
print OUT "Total CS: $total_cs\n";
#and finally, compute the two statistics

$ba = $total_h / $total_ab;

$s = $total_h - $total_2b - $total_3b - $total_hr;
$slg = ($s + 2*$total_2b + 3*$total_3b + 4*$total_hr) / $total_ab;

print OUT "Batting Average: $ba\n";
print  OUT "Slugging Percentage: $slg\n";

close OUT;
